示例#1
0
func (c *AuthorizeCommand) Run(args []string) int {
	apikey := c.ask("Enter your API Token:", "Input api token")
	fmt.Println(`Defaults can be changed at any time in your ~/.godo-cli/config.yaml configuration file.`)

	c.Config.Authentication.APIKey = apikey

	savePath, err := config.GetConfigPath()

	if err != nil {
		fmt.Errorf("Error GetConfigPath %s", err)
		return 1
	}

	err = config.SaveConfig(savePath, c.Config)

	if err != nil {
		fmt.Errorf("Error SaveConfig %s", err)
		return 1
	}

	fmt.Println("Authentication with DigitalOcean was successful!")
	return 0
}
示例#2
0
func main() {
	ui := &cli.BasicUi{Writer: os.Stdout}

	configPath, err := config.GetConfigPath()
	if err != nil {
		fmt.Errorf("Error GetConfigPath %s", err)
		os.Exit(1)
	}

	_, err = os.Stat(configPath)
	if err != nil {
		configDummy := &config.Config{}
		configDirPath, _ := config.GetConfigDirectory()

		if err := os.Mkdir(configDirPath, 0766); err != nil {
			fmt.Errorf("Error LoadConfig %s", err)
			os.Exit(1)
		}

		config.SaveConfig(configPath, configDummy)
	}

	config, err := config.LoadConfig(configPath)
	if err != nil {
		fmt.Errorf("Error LoadConfig %s", err)
		return
	}

	godoCli := getClinet(config.Authentication.APIKey)

	c := cli.NewCLI(ApplicationName, Version)
	c.Args = os.Args[1:]
	c.Commands = map[string]cli.CommandFactory{
		"authorize": func() (cli.Command, error) {
			return &command.AuthorizeCommand{
				Ui:     ui,
				Config: config,
				Client: godoCli,
			}, nil
		},
		"version": func() (cli.Command, error) {
			return &command.VersionCommand{
				Ui:      ui,
				AppName: ApplicationName,
				Version: Version,
			}, nil
		},
		"sizes": func() (cli.Command, error) {
			return &command.SizesCommand{
				Ui:     ui,
				Client: godoCli,
			}, nil
		},
		"regions": func() (cli.Command, error) {
			return &command.RegionsCommand{
				Ui:     ui,
				Client: godoCli,
			}, nil
		},
		"images": func() (cli.Command, error) {
			return &command.ImagesCommand{
				Ui:     ui,
				Client: godoCli,
			}, nil
		},
		"domains": func() (cli.Command, error) {
			return &command.DomainsCommand{
				Ui:     ui,
				Client: godoCli,
			}, nil
		},
		"droplets": func() (cli.Command, error) {
			return &command.DropletsCommand{
				Ui:     ui,
				Client: godoCli,
			}, nil
		},
		"keys": func() (cli.Command, error) {
			return &command.KeysCommand{
				Ui:     ui,
				Client: godoCli,
			}, nil
		},
		"account": func() (cli.Command, error) {
			return &command.AccountCommand{
				Ui:     ui,
				Client: godoCli,
			}, nil
		},
		"create": func() (cli.Command, error) {
			return &command.CreateCommand{
				Ui:     ui,
				Client: godoCli,
				Config: config,
			}, nil
		},
		"info": func() (cli.Command, error) {
			return &command.InfoCommand{
				Ui:     ui,
				Client: godoCli,
			}, nil
		},
		"destroy": func() (cli.Command, error) {
			return &command.DestroyCommand{
				Ui:     ui,
				Client: godoCli,
			}, nil
		},
		"config": func() (cli.Command, error) {
			return &command.ConfigCommand{
				Ui:     ui,
				Config: config,
				Client: godoCli,
			}, nil
		},
		"ssh": func() (cli.Command, error) {
			return &command.SSHCommand{
				Ui:     ui,
				Client: godoCli,
			}, nil
		},
		"power": func() (cli.Command, error) {
			return &command.PowerCommand{
				Ui:     ui,
				Client: godoCli,
			}, nil
		},
		"snapshot": func() (cli.Command, error) {
			return &command.SnapshotCommand{
				Ui:     ui,
				Client: godoCli,
			}, nil
		},
		"shutdown": func() (cli.Command, error) {
			return &command.ShutdownCommand{
				Ui:     ui,
				Client: godoCli,
			}, nil
		},
	}

	exitStatus, err := c.Run()
	if err != nil {
		fmt.Println(exitStatus, err)
	}
	os.Exit(exitStatus)
}