Example #1
0
func (d *Delete) Run(args []string) int {
	if len(d.Providers) != 1 {
		fmt.Print(d.Help())
		return 1
	}

	if flags.Has("help", args) {
		fmt.Print(d.Help())
		return 1
	}

	provider := d.Providers[0]
	if provider == "all" {
		fmt.Fprintln(os.Stderr, "Delete doesn't support multiple providers")
		return 1
	}

	p, remArgs, err := Provider(provider, args)
	if err != nil {
		fmt.Fprintln(os.Stderr, err.Error())
		return 1
	}

	deleter, ok := p.(Deleter)
	if !ok {
		err := fmt.Errorf("'%s' doesn't support deleting images", provider)
		fmt.Fprintln(os.Stderr, err.Error())
		return 1
	}

	// Don't ask for question if --force is enabled
	if !d.Force {
		response, err := d.Ui.Ask("Do you really want to delete? (Type 'yes' to continue):")
		if err != nil {
			fmt.Fprintln(os.Stderr, err.Error())
			return 1
		}

		if response != "yes" {
			d.Ui.Output("Delete cancelled.")
			return 0
		}
	}

	if err := deleter.Delete(remArgs); err != nil {
		fmt.Fprintln(os.Stderr, err.Error())
		return 1
	}

	return 0
}
Example #2
0
// Run shows a given resource.
func (s *Show) Run(args []string) int {
	if flags.Has("help", args) || len(args) < 1 {
		fmt.Print(s.Help())
		return 1
	}

	// process the subcommand and it's options
	switch args[0] {
	case "config":
		s.showConfig()
	case "jobs":
		s.AllJobs()
	default:
		fmt.Print("ERROR: invalid option for show\n")
		return 1
	}

	return 1
}
Example #3
0
func (c *Copy) Run(args []string) int {
	if len(c.Providers) != 1 {
		fmt.Print(c.Help())
		return 1
	}

	if flags.Has("help", args) {
		fmt.Print(c.Help())
		return 1
	}

	provider := c.Providers[0]
	if provider == "all" {
		fmt.Fprintln(os.Stderr, "Copy doesn't support multiple providers")
		return 1
	}

	p, remArgs, err := Provider(provider, args)
	if err != nil {
		fmt.Fprintln(os.Stderr, err.Error())
		return 1
	}

	copier, ok := p.(Copier)
	if !ok {
		err := fmt.Errorf("'%s' doesn't support copying images", provider)
		fmt.Fprintln(os.Stderr, err.Error())
		return 1
	}

	if err := copier.Copy(remArgs); err != nil {
		fmt.Fprintln(os.Stderr, err.Error())
		return 1
	}

	return 0
}
Example #4
0
func (m *Modify) Run(args []string) int {
	if len(m.Providers) != 1 {
		fmt.Print(m.Help())
		return 1
	}

	if flags.Has("help", args) {
		fmt.Print(m.Help())
		return 1
	}

	provider := m.Providers[0]
	if provider == "all" {
		fmt.Fprintln(os.Stderr, "Modify doesn't support multiple providers")
		return 1
	}

	p, remArgs, err := Provider(provider, args)
	if err != nil {
		fmt.Fprintln(os.Stderr, err.Error())
		return 1
	}

	mr, ok := p.(Modifier)
	if !ok {
		err := fmt.Errorf("'%s' doesn't support listing images", provider)
		fmt.Fprintln(os.Stderr, err.Error())
		return 1
	}

	if err := mr.Modify(remArgs); err != nil {
		fmt.Fprintln(os.Stderr, err.Error())
		return 1
	}

	return 0
}
Example #5
0
func (l *List) Run(args []string) int {
	if len(l.Providers) == 0 {
		fmt.Println(l.Help())
		return 1
	}

	if flags.Has("help", args) {
		fmt.Println(l.Help())
		return 1
	}

	if len(l.Providers) == 1 && l.Providers[0] == "all" {
		l.Providers = providerList
	}

	var (
		wg          sync.WaitGroup
		mu          sync.Mutex // protects multiErrors
		multiErrors error
	)

	printProvider := func(provider string) error {
		p, remArgs, err := Provider(provider, args)
		if err != nil {
			return err
		}

		lister, ok := p.(Lister)
		if !ok {
			return fmt.Errorf("Provider '%s' doesn't support listing images", provider)
		}

		if err := lister.List(remArgs); err != nil {
			// we don't return here, because Print might display at least
			// successfull results.
			return err
		}

		fmt.Println("")
		return nil
	}

	for _, provider := range l.Providers {
		wg.Add(1)
		go func(provider string) {
			err := printProvider(provider)
			if err != nil {
				err = fmt.Errorf("%s: %s", provider, err)
				mu.Lock()
				multiErrors = multierror.Append(multiErrors, err)
				mu.Unlock()
			}
			wg.Done()
		}(provider)
	}

	wg.Wait()

	if multiErrors != nil {
		fmt.Fprintln(os.Stderr, multiErrors.Error())
		return 1
	}

	return 0
}