func (r *List) Run(p writ.Path, positional []string) {
	if r.HelpFlag {
		p.Last().ExitHelp(nil)
	}
	// Listing operation omitted for brevity.  This would be a call to ioutil.ReadDir
	// followed by conditional formatting based on the r.LongFormat value.
}
Example #2
0
File: main.go Project: otm/limes
// Run is the main cli handler
func (g *Limes) Run(cmd *Limes, p writ.Path, positional []string) {
	if g.Version {
		fmt.Printf("limes %v compiled on %v\n", version, date)
		return
	}

	p.Last().ExitHelp(errors.New("COMMAND is required"))
}
Example #3
0
File: main.go Project: otm/limes
// Run is the handler for the status command
func (l *Status) Run(cmd *Limes, p writ.Path, positional []string) {
	if l.HelpFlag {
		p.Last().ExitHelp(nil)
	}

	rpc := newCliClient(cmd.Address)
	defer rpc.close()
	rpc.printStatus(l)
}
func (r *Link) Run(p writ.Path, positional []string) {
	if r.HelpFlag {
		p.Last().ExitHelp(nil)
	}
	if len(positional) != 2 {
		p.Last().ExitHelp(errors.New("ln requires two arguments, OLD and NEW"))
	}
	// Link operation omitted for brevity.  This would be os.Link or os.Symlink
	// based on the r.Symlink value.
}
Example #5
0
File: main.go Project: otm/limes
// Run is the handler for the start command
func (l *Start) Run(cmd *Limes, p writ.Path, positional []string) {
	if l.HelpFlag {
		p.Last().ExitHelp(nil)
	}

	if cmd.Profile == "" {
		cmd.Profile = profileDefault
	}
	StartService(cmd.ConfigFile, cmd.Address, cmd.Profile, l.MFA, l.Port, l.Fake)
}
Example #6
0
File: main.go Project: otm/limes
// Run is the handler for the profile command
func (l *SwitchProfile) Run(cmd *Limes, p writ.Path, positional []string) {
	if l.HelpFlag {
		p.Last().ExitHelp(nil)
	}

	if len(positional) != 1 {
		p.Last().ExitHelp(errors.New("profile name is required"))
	}

	rpc := newCliClient(cmd.Address)
	defer rpc.close()
	rpc.assumeRole(positional[0], "")
}
Example #7
0
File: main.go Project: otm/limes
// Run is the handler for the run command
func (l *RunCmd) Run(cmd *Limes, p writ.Path, positional []string) {
	if l.HelpFlag || len(positional) == 0 {
		p.Last().ExitHelp(nil)
	}

	command := exec.Command(positional[0], positional[1:]...)

	rpc := newCliClient(cmd.Address)
	defer rpc.close()

	if cmd.Profile != "" {
		creds, err := rpc.retreiveAWSEnv(cmd.Profile, "")
		if err != nil {
			fmt.Fprintf(errout, "error retreving profile: %v", err)
			os.Exit(1)
		}
		command.Env = append(os.Environ(),
			"AWS_ACCESS_KEY_ID="+creds.AccessKeyID,
			"AWS_SECRET_ACCESS_KEY="+creds.SecretAccessKey,
			"AWS_SESSION_TOKEN="+creds.SessionToken,
			"AWS_DEFAULT_REGION="+creds.Region,
			"AWS_REGION="+creds.Region,
		)
	} else {
		r, err := rpc.status()
		if err != nil || r.AccessKeyId == "" {
			fmt.Fprintf(errout, "error retreving profile: %v", err)
			os.Exit(1)
		}
		command.Env = append(os.Environ(),
			"AWS_ACCESS_KEY_ID="+r.AccessKeyId,
			"AWS_SECRET_ACCESS_KEY="+r.SecretAccessKey,
			"AWS_SESSION_TOKEN="+r.SessionToken,
			"AWS_DEFAULT_REGION="+r.Region,
			"AWS_REGION="+r.Region,
		)
	}

	command.Stdin = os.Stdin
	command.Stdout = os.Stdout
	command.Stderr = os.Stderr
	err := command.Run()
	if err != nil {
		// TODO: Handle exit error
	}
}
Example #8
0
File: main.go Project: otm/limes
// Run is the handler for the show subcommand
func (l *ShowCmd) Run(cmd *Limes, p writ.Path, positional []string) {
	options := []string{"profiles"}
	msg := fmt.Errorf("valid components: %v\n", strings.Join(options, ", "))

	if l.HelpFlag {
		p.Last().ExitHelp(msg)
	}

	if len(positional) == 0 {
		p.Last().ExitHelp(msg)
	}

	switch positional[0] {
	case "profiles":
		rpc := newCliClient(cmd.Address)
		defer rpc.close()
		roles, err := rpc.listRoles()
		if err != nil {
			fmt.Fprintf(os.Stderr, "%v\n", err)
			os.Exit(1)
		}
		fmt.Printf("%v\n", strings.Join(roles, "\n"))
	default:
		p.Last().ExitHelp(msg)
	}
}
Example #9
0
File: main.go Project: otm/limes
// Run is the handler for the env subcommand
func (l *Env) Run(cmd *Limes, p writ.Path, positional []string) {
	if l.HelpFlag {
		p.Last().ExitHelp(nil)
	}

	profile := ""
	if cmd.Profile != "" {
		profile = cmd.Profile
	}

	if len(positional) == 1 {
		profile = positional[0]
	}

	rpc := newCliClient(cmd.Address)
	defer rpc.close()

	if profile == "" {
		r, err := rpc.status()
		if err != nil {
			p.Last().ExitHelp(fmt.Errorf(lookupCorrection(err)))
		}
		profile = r.Role
		if profile == "" {
			p.Last().ExitHelp(nil)
		}
	}

	credentials, err := rpc.retreiveAWSEnv(profile, "")
	if err != nil {
		fmt.Fprintf(errout, "error retreiving profile: %v\n", err)
		os.Exit(1)
	}
	fmt.Fprintf(out, "export AWS_ACCESS_KEY_ID=%v\n", credentials.AccessKeyID)
	fmt.Fprintf(out, "export AWS_SECRET_ACCESS_KEY=%v\n", credentials.SecretAccessKey)
	fmt.Fprintf(out, "export AWS_SESSION_TOKEN=%v\n", credentials.SessionToken)
	fmt.Fprintf(out, "export AWS_DEFAULT_REGION=%v\n", credentials.Region)
	fmt.Fprintf(out, "export AWS_REGION=%v\n", credentials.Region)
	fmt.Fprintf(out, "# Run this command to configure your shell:\n")
	fmt.Fprintf(out, "# eval \"$(limes env %s)\"\n", profile)
}
Example #10
0
func (r *GoBox) Run(p writ.Path, positional []string) {
	// The user didn't specify a subcommand.  Give them help.
	p.Last().ExitHelp(errors.New("COMMAND is required"))
}
Example #11
0
File: main.go Project: otm/limes
// Run is the handler for the fix command
func (l *Fix) Run(cmd *Limes, p writ.Path, positional []string) {
	if l.HelpFlag {
		p.Last().ExitHelp(nil)
	}

	home, err := homeDir()
	if err != nil {
		fmt.Fprintf(errout, "unable to fetch user information: %v\n", err)
		os.Exit(1)
	}

	exitOnErr := func(err error) {
		if err != nil {
			fmt.Fprintf(errout, "error: %v\n", err)
			os.Exit(1)
		}
	}

	exists := func(path string) bool {
		_, staterr := os.Stat(path)
		if staterr != nil {
			return false
		}
		return true
	}

	if l.Restore == true {
		originalPath := filepath.Join(home, awsConfDir, awsCredentialsFile)
		movedPath := filepath.Join(home, awsConfDir, awsRenamePrefix+awsCredentialsFile)
		fmt.Printf("loooking for: %v\n", movedPath)
		if exists(movedPath) {
			fmt.Fprintf(out, "# restoring: %v\n", originalPath)
			exitOnErr(os.Rename(movedPath, originalPath))
		}

		originalPath = filepath.Join(home, awsConfDir, awsConfigFile)
		movedPath = filepath.Join(home, awsConfDir, awsRenamePrefix+awsConfigFile)
		fmt.Printf("loooking for: %v\n", movedPath)
		if exists(movedPath) {
			fmt.Fprintf(out, "# restoring: %v\n", originalPath)
			exitOnErr(os.Rename(movedPath, originalPath))
		}
		return
	}

	if err = checkActiveAWSConfig(); err == nil {
		fmt.Fprintf(out, "# configuration ok, nothing to fix\n")
		os.Exit(0)
	}

	for err := checkActiveAWSConfig(); err != nil; err = checkActiveAWSConfig() {
		switch err {
		case errKeyPairInAWSCredentialsFile:
			originalPath := filepath.Join(home, awsConfDir, awsCredentialsFile)
			movedPath := filepath.Join(home, awsConfDir, awsRenamePrefix+awsCredentialsFile)
			fmt.Fprintf(out, "# moving: %v => %v\n", originalPath, movedPath)
			exitOnErr(os.Rename(originalPath, movedPath))
		case errKeyPairInAWSConfigFile:
			originalPath := filepath.Join(home, awsConfDir, awsConfigFile)
			movedPath := filepath.Join(home, awsConfDir, awsRenamePrefix+awsConfigFile)
			fmt.Fprintf(out, "# moving: %v => %v\n", originalPath, movedPath)
			exitOnErr(os.Rename(originalPath, movedPath))
		case errActiveAWSEnvironment:
			fmt.Fprintf(out, "# You have active AWS Environment variables\n")
			fmt.Fprintf(out, "# Either run the code bellow in your shell or excute it with\n")
			fmt.Fprintf(out, "# eval \"$(limes fix)\"\n")
			fmt.Fprintf(out, "unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN")
		default:
			fmt.Fprintf(out, "unable to fix: %v\n", err)
		}
	}
}