in cobra/controller/variable.go [100:209]
func variableRun(cmd *cobra.Command, args []string) error {
logrus.Tracef("start: variableRun")
// organization token manages variables on workspace
// https://www.terraform.io/docs/cloud/users-teams-organizations/api-tokens.html#team-api-tokens
token := dao.GetOrganizationToken(profile)
client := aid.GetTFEClient(token)
fArg := args[0]
switch fArg {
case "list":
workspaceID := helper.GetCmdFlagString(cmd, "workspace-id")
list, err := variableList(client, workspaceID, tfe.VariableListOptions{})
if err == nil && len(list.Items) > 0 {
aid.PrintVariableList(list)
} else {
return fmt.Errorf("no variable was found")
}
case "create":
workspaceID := helper.GetCmdFlagString(cmd, "workspace-id")
options := aid.GetVariableCreateOptions(cmd)
variable, err := variableCreate(client, workspaceID, options)
if err == nil && variable.ID != "" {
fmt.Println(aid.ToJSON(variable))
} else {
return fmt.Errorf("unable to create variable\n%v", err)
}
case "read":
workspaceID := helper.GetCmdFlagString(cmd, "workspace-id")
id := helper.GetCmdFlagString(cmd, "id")
variable, err := variableRead(client, workspaceID, id)
if err == nil {
fmt.Println(aid.ToJSON(variable))
} else {
return fmt.Errorf("variable %s not found\n%v", id, err)
}
case "update":
workspaceID := helper.GetCmdFlagString(cmd, "workspace-id")
id := helper.GetCmdFlagString(cmd, "id")
options := aid.GetVariableUpdateOptions(cmd)
variable, err := variableUpdate(client, workspaceID, id, options)
if err == nil && variable.ID != "" {
fmt.Println(aid.ToJSON(variable))
} else {
return fmt.Errorf("unable to update variable\n%v", err)
}
case "update-by-key":
workspaceID := helper.GetCmdFlagString(cmd, "workspace-id")
list, err := variableList(client, workspaceID, tfe.VariableListOptions{})
if err != nil && len(list.Items) == 0 {
return fmt.Errorf("variable list is empty")
}
found, err := aid.FindVariableByKey(list, cmd)
if err != nil {
return err
}
options := aid.GetVariableUpdateOptions(cmd)
variable, err := variableUpdate(client, workspaceID, found.ID, options)
if err == nil && variable.ID != "" {
fmt.Println(aid.ToJSON(variable))
} else {
return fmt.Errorf("unable to update variable\n%v", err)
}
case "delete":
workspaceID := helper.GetCmdFlagString(cmd, "workspace-id")
id := helper.GetCmdFlagString(cmd, "id")
err := variableDelete(client, workspaceID, id)
if err == nil {
fmt.Printf("variable %s deleted successfully\n", id)
} else {
return fmt.Errorf("unable to delete variable %s\n%v", id, err)
}
case "delete-all":
workspaceID := helper.GetCmdFlagString(cmd, "workspace-id")
list, err := variableList(client, workspaceID, tfe.VariableListOptions{})
if err != nil {
return fmt.Errorf("no variable was found\n%v", err)
}
for _, v := range list.Items {
fmt.Printf("attempting to delete variable %s (%s)\n", v.Key, v.ID)
err := variableDelete(client, workspaceID, v.ID)
if err != nil {
return fmt.Errorf("unable to delete variable %s (%s)\n%v", v.Key, v.ID, err)
}
fmt.Printf("variable %s (%s) deleted successfully\n", v.Key, v.ID)
}
default:
return fmt.Errorf("unknown argument provided")
}
return nil
}