Skip to content

Commit

Permalink
stop using so many pointers - terraform needs work
Browse files Browse the repository at this point in the history
  • Loading branch information
Taimoor Ahmad committed Jan 22, 2024
1 parent fdfb631 commit 84e68c4
Show file tree
Hide file tree
Showing 20 changed files with 179 additions and 151 deletions.
4 changes: 2 additions & 2 deletions src/cmd/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ EOF`,
case "webhook":
input, err := readResourceInput[opslevel.CustomActionsWebhookActionCreateInput]()
cobra.CheckErr(err)
result, err := getClientGQL().CreateWebhookAction(*input)
result, err := getClientGQL().CreateWebhookAction(input)
cobra.CheckErr(err)
fmt.Printf("created webhook action: %s\n", result.Id)
default:
Expand Down Expand Up @@ -127,7 +127,7 @@ EOF`,
input, err := readResourceInput[opslevel.CustomActionsWebhookActionUpdateInput]()
input.Id = *opslevel.NewID(key)
cobra.CheckErr(err)
action, err := getClientGQL().UpdateWebhookAction(*input)
action, err := getClientGQL().UpdateWebhookAction(input)
cobra.CheckErr(err)
common.JsonPrint(json.MarshalIndent(action, "", " "))
default:
Expand Down
10 changes: 5 additions & 5 deletions src/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ EOF`,
log.Warn().Msg("running noninteractively since using heredoc")
usePrompts = false
}
check, err := createCheck(*input, usePrompts)
check, err := createCheck(input, usePrompts)
cobra.CheckErr(err)
fmt.Printf("Created Check '%s' with id '%s'\n", check.Name, check.Id)
},
Expand Down Expand Up @@ -119,7 +119,7 @@ EOF`,
log.Warn().Msg("running noninteractively since using heredoc")
usePrompts = false
}
check, err := updateCheck(*input, usePrompts)
check, err := updateCheck(input, usePrompts)
cobra.CheckErr(err)
common.JsonPrint(json.MarshalIndent(check, "", " "))
},
Expand Down Expand Up @@ -453,14 +453,14 @@ func (checkInputType *CheckInputType) IsUpdateInput() bool {
return ok
}

func readCheckInput() (*CheckInputType, error) {
func readCheckInput() (CheckInputType, error) {
input, err := readResourceInput[CheckInputType]()
fmt.Println(input)
if err != nil {
return nil, err
return input, err
}
if input.Version != CheckConfigCurrentVersion {
return nil, fmt.Errorf("supported config version is '%s' but found '%s' | please update config file",
return input, fmt.Errorf("supported config version is '%s' but found '%s' | please update config file",
CheckConfigCurrentVersion, input.Version)
}
return input, nil
Expand Down
9 changes: 5 additions & 4 deletions src/cmd/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ EOF
Run: func(cmd *cobra.Command, args []string) {
input, err := readCreateServiceDependencyInput()
cobra.CheckErr(err)
result, err := getClientGQL().CreateServiceDependency(*input)
result, err := getClientGQL().CreateServiceDependency(input)
cobra.CheckErr(err)
fmt.Println(result.Id)
},
Expand Down Expand Up @@ -71,12 +71,13 @@ func init() {
deleteServiceCmd.AddCommand(deleteServiceDependencyCmd)
}

func readCreateServiceDependencyInput() (*opslevel.ServiceDependencyCreateInput, error) {
func readCreateServiceDependencyInput() (opslevel.ServiceDependencyCreateInput, error) {
var output opslevel.ServiceDependencyCreateInput
in, err := readResourceInput[CLIServiceDependencyCreateInput]()
if err != nil {
return nil, err
return output, err
}
output := &opslevel.ServiceDependencyCreateInput{
output = opslevel.ServiceDependencyCreateInput{
DependencyKey: opslevel.ServiceDependencyKey{
SourceIdentifier: opslevel.NewIdentifier(in.Source),
DestinationIdentifier: opslevel.NewIdentifier(in.Target),
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ EOF
Run: func(cmd *cobra.Command, args []string) {
input, err := readResourceInput[opslevel.DomainInput]()
cobra.CheckErr(err)
result, err := getClientGQL().CreateDomain(*input)
result, err := getClientGQL().CreateDomain(input)
cobra.CheckErr(err)
fmt.Println(result.Id)
},
Expand Down Expand Up @@ -136,7 +136,7 @@ EOF
key := args[0]
input, err := readResourceInput[opslevel.DomainInput]()
cobra.CheckErr(err)
domain, err := getClientGQL().UpdateDomain(key, *input)
domain, err := getClientGQL().UpdateDomain(key, input)
cobra.CheckErr(err)
fmt.Println(domain.Id)
},
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ EOF`,
Run: func(cmd *cobra.Command, args []string) {
input, err := readResourceInput[opslevel.FilterCreateInput]()
cobra.CheckErr(err)
result, err := getClientGQL().CreateFilter(*input)
result, err := getClientGQL().CreateFilter(input)
cobra.CheckErr(err)
fmt.Println(result.Id)
},
Expand Down
9 changes: 5 additions & 4 deletions src/cmd/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ EOF`,
Run: func(cmd *cobra.Command, args []string) {
input, err := readInfraInput()
cobra.CheckErr(err)
result, err := getClientGQL().CreateInfrastructure(*input)
result, err := getClientGQL().CreateInfrastructure(input)
cobra.CheckErr(err)
fmt.Println(result.Id)
},
Expand Down Expand Up @@ -211,7 +211,7 @@ EOF`,
key := args[0]
input, err := readInfraInput()
cobra.CheckErr(err)
result, err := getClientGQL().UpdateInfrastructure(key, *input)
result, err := getClientGQL().UpdateInfrastructure(key, input)
cobra.CheckErr(err)
fmt.Println(result.Id)
},
Expand Down Expand Up @@ -243,10 +243,11 @@ func init() {
deleteCmd.AddCommand(deleteInfraCmd)
}

func readInfraInput() (*opslevel.InfraInput, error) {
// TODO: why is this special?
func readInfraInput() (opslevel.InfraInput, error) {
file, err := io.ReadAll(os.Stdin)
cobra.CheckErr(err)
evt := &opslevel.InfraInput{}
evt := opslevel.InfraInput{}
cobra.CheckErr(yaml.Unmarshal(file, &evt))
return evt, nil
}
11 changes: 5 additions & 6 deletions src/cmd/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package cmd

import (
"bytes"
"os"

"github.com/rs/zerolog/log"
"os"

"github.com/spf13/viper"
"gopkg.in/yaml.v3"
Expand All @@ -30,7 +29,7 @@ func readInputConfig() {
viper.ReadInConfig()
}

func readResourceInput[T any]() (*T, error) {
func readResourceInput[T any]() (T, error) {
var err error
var resource T
var yamlData []byte
Expand All @@ -49,13 +48,13 @@ func readResourceInput[T any]() (*T, error) {
yamlData, err = os.ReadFile(dataFile)
}
if err != nil {
return nil, err
return resource, err
}

if err := yaml.Unmarshal(yamlData, &resource); err != nil {
return nil, err
return resource, err
}
return &resource, nil
return resource, nil
}

func isStdInFromTerminal() bool {
Expand Down
22 changes: 11 additions & 11 deletions src/cmd/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ EOF`, getYaml[opslevel.PropertyInput]()),
Run: func(cmd *cobra.Command, args []string) {
input, err := readResourceInput[opslevel.PropertyInput]()
cobra.CheckErr(err)
newProperty, err := getClientGQL().PropertyAssign(*input)
newProperty, err := getClientGQL().PropertyAssign(input)
cobra.CheckErr(err)

fmt.Printf("assigned property '%s' on '%s'\n", newProperty.Definition.Id, newProperty.Owner.Id())
Expand Down Expand Up @@ -143,7 +143,7 @@ EOF`, getYaml[opslevel.PropertyDefinitionInput]()),
Run: func(cmd *cobra.Command, args []string) {
input, err := readPropertyDefinitionInput()
cobra.CheckErr(err)
newPropertyDefinition, err := getClientGQL().CreatePropertyDefinition(*input)
newPropertyDefinition, err := getClientGQL().CreatePropertyDefinition(input)
cobra.CheckErr(err)

fmt.Println(newPropertyDefinition.Id)
Expand All @@ -165,7 +165,7 @@ EOF`, getYaml[opslevel.PropertyDefinitionInput]()),
identifier := args[0]
input, err := readPropertyDefinitionInput()
cobra.CheckErr(err)
result, err := getClientGQL().UpdatePropertyDefinition(identifier, *input)
result, err := getClientGQL().UpdatePropertyDefinition(identifier, input)
cobra.CheckErr(err)

if isYamlOutput() {
Expand All @@ -177,22 +177,22 @@ EOF`, getYaml[opslevel.PropertyDefinitionInput]()),
}

// The schema in PropertyDefinitionInput can be a nested map[string]any and needs to be handled separately
func readPropertyDefinitionInput() (*opslevel.PropertyDefinitionInput, error) {
d, err := readResourceInput[opslevel.JSONSchema]()
func readPropertyDefinitionInput() (opslevel.PropertyDefinitionInput, error) {
var propDefInput opslevel.PropertyDefinitionInput
data, err := readResourceInput[opslevel.JSONSchema]()
if err != nil {
return nil, err
return propDefInput, err
}
data := *d
name, ok := data["name"].(string)
if !ok {
return nil, fmt.Errorf("name is required and must be a string")
return propDefInput, fmt.Errorf("name is required and must be a string")
}
schema, ok := data["schema"].(map[string]any)
if !ok {
return nil, fmt.Errorf("schema is required and must be a JSON object")
return propDefInput, fmt.Errorf("schema is required and must be a JSON object")
}
jsonSchema := opslevel.JSONSchema(schema)
propDefInput := opslevel.PropertyDefinitionInput{
propDefInput = opslevel.PropertyDefinitionInput{
Name: opslevel.RefOf(name),
Schema: &jsonSchema,
}
Expand All @@ -204,7 +204,7 @@ func readPropertyDefinitionInput() (*opslevel.PropertyDefinitionInput, error) {
propDefInput.PropertyDisplayStatus = opslevel.RefOf(opslevel.PropertyDisplayStatusEnum(propertyDisplayStatus))
}

return &propDefInput, nil
return propDefInput, nil
}

var getPropertyDefinitionCmd = &cobra.Command{
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ EOF`,
Run: func(cmd *cobra.Command, args []string) {
input, err := readResourceInput[opslevel.ScorecardInput]()
cobra.CheckErr(err)
result, err := getClientGQL().CreateScorecard(*input)
result, err := getClientGQL().CreateScorecard(input)
cobra.CheckErr(err)
fmt.Printf("created scorecard: %s\n", result.Id)
},
Expand Down Expand Up @@ -98,7 +98,7 @@ EOF`,
key := args[0]
input, err := readResourceInput[opslevel.ScorecardInput]()
cobra.CheckErr(err)
scorecard, err := getClientGQL().UpdateScorecard(key, *input)
scorecard, err := getClientGQL().UpdateScorecard(key, input)
cobra.CheckErr(err)
common.JsonPrint(json.MarshalIndent(scorecard, "", " "))
},
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ EOF`,
Run: func(cmd *cobra.Command, args []string) {
input, err := readResourceInput[opslevel.SecretInput]()
cobra.CheckErr(err)
newSecret, err := getClientGQL().CreateSecret(secretAlias, *input)
newSecret, err := getClientGQL().CreateSecret(secretAlias, input)
cobra.CheckErr(err)

fmt.Println(newSecret.ID)
Expand Down Expand Up @@ -104,7 +104,7 @@ EOF`,
secretId := args[0]
input, err := readResourceInput[opslevel.SecretInput]()
cobra.CheckErr(err)
secret, err := getClientGQL().UpdateSecret(secretId, *input)
secret, err := getClientGQL().UpdateSecret(secretId, input)
cobra.CheckErr(err)
fmt.Println(secret.ID)
},
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ EOF`,
Run: func(cmd *cobra.Command, args []string) {
input, err := readResourceInput[opslevel.ServiceCreateInput]()
cobra.CheckErr(err)
result, err := getClientGQL().CreateService(*input)
result, err := getClientGQL().CreateService(input)
cobra.CheckErr(err)
common.PrettyPrint(result.Id)
},
Expand Down Expand Up @@ -120,7 +120,7 @@ EOF`,
Run: func(cmd *cobra.Command, args []string) {
input, err := readResourceInput[opslevel.ServiceUpdateInput]()
cobra.CheckErr(err)
service, err := getClientGQL().UpdateService(*input)
service, err := getClientGQL().UpdateService(input)
cobra.CheckErr(err)
common.JsonPrint(json.MarshalIndent(service, "", " "))
},
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var createSystemCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
input, err := readResourceInput[opslevel.SystemInput]()
cobra.CheckErr(err)
result, err := getClientGQL().CreateSystem(*input)
result, err := getClientGQL().CreateSystem(input)
cobra.CheckErr(err)
fmt.Println(result.Id)
},
Expand Down Expand Up @@ -118,7 +118,7 @@ var updateSystemCmd = &cobra.Command{
key := args[0]
input, err := readResourceInput[opslevel.SystemInput]()
cobra.CheckErr(err)
system, err := getClientGQL().UpdateSystem(key, *input)
system, err := getClientGQL().UpdateSystem(key, input)
cobra.CheckErr(err)
fmt.Println(system.Id)
},
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ EOF`,
input, err := readResourceInput[opslevel.TeamCreateInput]()
input.Name = key
cobra.CheckErr(err)
team, err := getClientGQL().CreateTeam(*input)
team, err := getClientGQL().CreateTeam(input)
cobra.CheckErr(err)
fmt.Println(team.Id)
},
Expand Down Expand Up @@ -154,7 +154,7 @@ EOF
input, err := readResourceInput[opslevel.TeamUpdateInput]()
input.Id = opslevel.NewID(key)
cobra.CheckErr(err)
team, err := getClientGQL().UpdateTeam(*input)
team, err := getClientGQL().UpdateTeam(input)
cobra.CheckErr(err)
fmt.Println(team.Id)
},
Expand Down
Loading

0 comments on commit 84e68c4

Please sign in to comment.