func sshKeyRun()

in cobra/controller/sshKey.go [105:183]


func sshKeyRun(cmd *cobra.Command, args []string) error {
	// config, err := cmd.Flags().GetString("config")
	// if err != nil {
	// 	return fmt.Errorf("unable to get flag config\n%v", err)
	// }

	// aid.LoadViper(config)

	token := dao.GetTeamToken(profile)
	client := aid.GetTFEClient(token)

	var sshKey *tfe.SSHKey
	var err error

	fArg := args[0]
	switch fArg {
	case "list":
		organization := dao.GetOrganization(profile)
		list, err := sshKeyList(client, organization)
		if err == nil {
			fmt.Println(aid.ToJSON(list))
		} else {
			return fmt.Errorf("no ssh key was found")
		}

	case "create":
		organization := dao.GetOrganization(profile)
		options := aid.GetSSHKeysCreateOptions(cmd)
		sshKey, err = sshKeyCreate(client, organization, options)
		if err != nil {
			logrus.Errorln("unable to create ssh key")
			return err
		}

		if err == nil && sshKey.ID != "" {
			fmt.Println(aid.ToJSON(sshKey))
		}
	case "read":
		id, err := cmd.Flags().GetString("id")
		if err != nil {
			return fmt.Errorf("unable to get flag id\n%v", err)
		}

		sshKey, err := sshKeyRead(client, id)
		if err == nil {
			fmt.Println(aid.ToJSON(sshKey))
		} else {
			return fmt.Errorf("ssh key %s not found\n%v", id, err)
		}

	case "update":
		id, err := cmd.Flags().GetString("id")
		if err != nil {
			return fmt.Errorf("unable to get flag id\n%v", err)
		}

		options := aid.GetSSHKeysUpdateOptions(cmd)
		sshKey, err = sshKeyUpdate(client, id, options)
		if err == nil && sshKey.ID != "" {
			fmt.Println(aid.ToJSON(sshKey))
		} else {
			return fmt.Errorf("unable to update ssh key\n%v", err)
		}
	case "delete":
		id, err := cmd.Flags().GetString("id")
		if err != nil {
			return fmt.Errorf("unable to get flag id\n%v", err)
		}

		err = sshKeyDelete(client, id)
		if err == nil {
			cmd.Printf("ssh key %s deleted successfully\n", id)
		} else {
			return fmt.Errorf("unable to delete ssh key %s\n%v", id, err)
		}
	}

	return nil
}