Skip to content

Commit

Permalink
int32 database identifiers are evil!
Browse files Browse the repository at this point in the history
This starts migrating away from that.
  • Loading branch information
JAORMX committed Sep 28, 2023
1 parent 1f28744 commit 304bbf3
Show file tree
Hide file tree
Showing 45 changed files with 547 additions and 478 deletions.
4 changes: 2 additions & 2 deletions cmd/cli/app/artifact/artifact_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var artifact_getCmd = &cobra.Command{
},
RunE: func(cmd *cobra.Command, args []string) error {
tag := util.GetConfigValue("tag", "tag", cmd, "").(string)
artifactID := viper.GetInt32("id")
artifactID := viper.GetString("id")
latest_versions := viper.GetInt32("latest-versions")

// tag and latest versions cannot be set at same time
Expand Down Expand Up @@ -72,7 +72,7 @@ var artifact_getCmd = &cobra.Command{

func init() {
ArtifactCmd.AddCommand(artifact_getCmd)
artifact_getCmd.Flags().Int32P("id", "i", 0, "ID of the artifact to get info from")
artifact_getCmd.Flags().StringP("id", "i", "", "ID of the artifact to get info from")
artifact_getCmd.Flags().Int32P("latest-versions", "v", 1, "Latest artifact versions to retrieve")
artifact_getCmd.Flags().StringP("tag", "", "", "Specific artifact tag to retrieve")
if err := artifact_getCmd.MarkFlagRequired("id"); err != nil {
Expand Down
6 changes: 4 additions & 2 deletions cmd/cli/app/artifact/artifact_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ var artifact_listCmd = &cobra.Command{

for _, artifact_item := range artifacts.Results {
table.Append([]string{
fmt.Sprintf("%d", artifact_item.ArtifactPk), artifact_item.Type,
artifact_item.GetOwner(), artifact_item.GetName(),
artifact_item.ArtifactPk,
artifact_item.Type,
artifact_item.GetOwner(),
artifact_item.GetName(),
artifact_item.Repository,
artifact_item.Visibility,
artifact_item.CreatedAt.AsTime().Format(time.RFC3339),
Expand Down
5 changes: 2 additions & 3 deletions cmd/cli/app/policy/policy_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ mediator control plane.`,
},
Run: func(cmd *cobra.Command, args []string) {
// delete the policy via GRPC
id := util.GetConfigValue("id", "id", cmd, int32(0)).(int32)

id := viper.GetString("id")
provider := viper.GetString("provider")

conn, err := util.GrpcForCommand(cmd)
Expand All @@ -65,7 +64,7 @@ mediator control plane.`,

func init() {
PolicyCmd.AddCommand(policy_deleteCmd)
policy_deleteCmd.Flags().Int32P("id", "i", 0, "id of policy to delete")
policy_deleteCmd.Flags().StringP("id", "i", "", "id of policy to delete")
policy_deleteCmd.Flags().StringP("provider", "p", "github", "Provider for the policy")
err := policy_deleteCmd.MarkFlagRequired("id")
util.ExitNicelyOnError(err, "Error marking flag as required")
Expand Down
4 changes: 2 additions & 2 deletions cmd/cli/app/policy/policy_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ mediator control plane.`,
ctx, cancel := util.GetAppContext()
defer cancel()

id := viper.GetInt32("id")
id := viper.GetString("id")
policy, err := client.GetPolicyById(ctx, &pb.GetPolicyByIdRequest{
Context: &pb.Context{
Provider: provider,
Expand Down Expand Up @@ -84,7 +84,7 @@ mediator control plane.`,

func init() {
PolicyCmd.AddCommand(policy_getCmd)
policy_getCmd.Flags().Int32P("id", "i", 0, "ID for the policy to query")
policy_getCmd.Flags().StringP("id", "i", "", "ID for the policy to query")
policy_getCmd.Flags().StringP("output", "o", app.Table, "Output format (json, yaml or table)")
policy_getCmd.Flags().StringP("provider", "p", "github", "Provider for the policy")
// TODO set up group if specified
Expand Down
4 changes: 1 addition & 3 deletions cmd/cli/app/policy/table_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package policy

import (
"fmt"

"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"google.golang.org/protobuf/types/known/structpb"
Expand Down Expand Up @@ -80,7 +78,7 @@ func renderRuleTable(
def := marshalStructOrEmpty(rule.Def)

row := []string{
fmt.Sprintf("%d", *p.Id),
*p.Id,
p.Name,
p.Context.Provider,
entType.String(),
Expand Down
24 changes: 12 additions & 12 deletions cmd/cli/app/policy_status/policy_status_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ mediator control plane for an specific provider/group or policy id, entity type

provider := viper.GetString("provider")
group := viper.GetString("group")
policyId := viper.GetInt32("policy")
entityId := viper.GetInt32("entity")
policyId := viper.GetString("policy")
entityId := viper.GetString("entity")
entityType := viper.GetString("entity-type")
format := viper.GetString("output")

Expand All @@ -66,14 +66,6 @@ mediator control plane for an specific provider/group or policy id, entity type
return fmt.Errorf("provider must be set")
}

if policyId == 0 {
return fmt.Errorf("policy-id must be set")
}

if entityId == 0 {
return fmt.Errorf("entity-id must be set")
}

req := &pb.GetPolicyStatusByIdRequest{
Context: &pb.Context{
Provider: provider,
Expand Down Expand Up @@ -117,9 +109,17 @@ func init() {
PolicyStatusCmd.AddCommand(policystatus_getCmd)
policystatus_getCmd.Flags().StringP("provider", "p", "github", "Provider to get policy status for")
policystatus_getCmd.Flags().StringP("group", "g", "", "group id to get policy status for")
policystatus_getCmd.Flags().Int32P("policy", "i", 0, "policy id to get policy status for")
policystatus_getCmd.Flags().StringP("policy", "i", "", "policy id to get policy status for")
policystatus_getCmd.Flags().StringP("entity-type", "t", "",
fmt.Sprintf("the entity type to get policy status for (one of %s)", entities.KnownTypesCSV()))
policystatus_getCmd.Flags().Int32P("entity", "e", 0, "entity id to get policy status for")
policystatus_getCmd.Flags().StringP("entity", "e", "", "entity id to get policy status for")
policystatus_getCmd.Flags().StringP("output", "o", app.Table, "Output format (json, yaml or table)")

// mark as required
if err := policystatus_getCmd.MarkFlagRequired("policy"); err != nil {
util.ExitNicelyOnError(err, "error marking flag as required")
}
if err := policystatus_getCmd.MarkFlagRequired("entity"); err != nil {
util.ExitNicelyOnError(err, "error marking flag as required")
}
}
13 changes: 7 additions & 6 deletions cmd/cli/app/policy_status/policy_status_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ mediator control plane for an specific provider/group or policy id.`,

provider := viper.GetString("provider")
group := viper.GetString("group")
policyId := viper.GetInt32("policy")
policyId := viper.GetString("policy")
format := viper.GetString("output")
all := viper.GetBool("detailed")

Expand All @@ -64,10 +64,6 @@ mediator control plane for an specific provider/group or policy id.`,
return fmt.Errorf("provider must be set")
}

if policyId == 0 {
return fmt.Errorf("policy-id must be set")
}

req := &pb.GetPolicyStatusByIdRequest{
Context: &pb.Context{
Provider: provider,
Expand Down Expand Up @@ -112,9 +108,14 @@ func init() {
PolicyStatusCmd.AddCommand(policystatus_listCmd)
policystatus_listCmd.Flags().StringP("provider", "p", "github", "Provider to list policy status for")
policystatus_listCmd.Flags().StringP("group", "g", "", "group id to list policy status for")
policystatus_listCmd.Flags().Int32P("policy", "i", 0, "policy id to list policy status for")
policystatus_listCmd.Flags().StringP("policy", "i", "", "policy id to list policy status for")
policystatus_listCmd.Flags().StringP("output", "o", app.Table, "Output format (json, yaml or table)")
policystatus_listCmd.Flags().BoolP("detailed", "d", false, "List all policy violations")

if err := policystatus_listCmd.MarkFlagRequired("policy"); err != nil {
fmt.Fprintf(os.Stderr, "Error marking flag as required: %s\n", err)
os.Exit(1)
}
}

func handlePolicyStatusListTable(cmd *cobra.Command, resp *pb.GetPolicyStatusByIdResponse) {
Expand Down
7 changes: 3 additions & 4 deletions cmd/cli/app/policy_status/table_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package policy_status

import (
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -52,7 +51,7 @@ func renderPolicyStatusTable(
table *tablewriter.Table,
) {
row := []string{
fmt.Sprintf("%d", ps.PolicyId),
ps.PolicyId,
ps.PolicyName,
getStatusText(ps.PolicyStatus),
ps.LastUpdated.AsTime().Format(time.RFC3339),
Expand Down Expand Up @@ -84,8 +83,8 @@ func renderRuleEvaluationStatusTable(
table *tablewriter.Table,
) {
row := []string{
fmt.Sprintf("%d", reval.PolicyId),
fmt.Sprintf("%d", reval.RuleId),
reval.PolicyId,
reval.RuleId,
reval.RuleName,
reval.Entity,
getStatusText(reval.Status),
Expand Down
10 changes: 5 additions & 5 deletions cmd/cli/app/repo/repo_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ var repo_getCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {

provider := util.GetConfigValue("provider", "provider", cmd, "").(string)
repoid := viper.GetInt32("repo-id")
repoid := viper.GetString("repo-id")
format := viper.GetString("output")
name := util.GetConfigValue("name", "name", cmd, "").(string)

// if name is set, repo-id cannot be set
if name != "" && repoid != 0 {
if name != "" && repoid != "" {
return fmt.Errorf("cannot set both name and repo-id")
}

// either name or repoid needs to be set
if name == "" && repoid == 0 {
if name == "" && repoid == "" {
return fmt.Errorf("either name or repo-id needs to be set")
}

Expand All @@ -86,7 +86,7 @@ var repo_getCmd = &cobra.Command{

// check repo by id
var repository *pb.RepositoryRecord
if repoid != 0 {
if repoid != "" {
resp, err := client.GetRepositoryById(ctx, &pb.GetRepositoryByIdRequest{
RepositoryId: repoid,
})
Expand Down Expand Up @@ -127,6 +127,6 @@ func init() {
repo_getCmd.Flags().StringP("output", "f", "", "Output format (json or yaml)")
repo_getCmd.Flags().StringP("provider", "p", "", "Name for the provider to enroll")
repo_getCmd.Flags().StringP("name", "n", "", "Name of the repository (owner/name format)")
repo_getCmd.Flags().Int32P("repo-id", "r", 0, "ID of the repo to query")
repo_getCmd.Flags().StringP("repo-id", "r", "", "ID of the repo to query")
repo_getCmd.Flags().BoolP("status", "s", false, "Only return the status of the policies associated to this repo")
}
2 changes: 1 addition & 1 deletion cmd/cli/app/repo/repo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var repo_listCmd = &cobra.Command{

for _, v := range resp.Results {
row := []string{
fmt.Sprintf("%d", v.Id),
v.Id,
fmt.Sprintf("%d", v.GroupId),
fmt.Sprintf("%d", v.GetRepoId()),
fmt.Sprintf("%s/%s", v.GetOwner(), v.GetName()),
Expand Down
4 changes: 2 additions & 2 deletions cmd/cli/app/rule_type/rule_type_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mediator control plane.`,
},
Run: func(cmd *cobra.Command, args []string) {
// delete the policy via GRPC
id := util.GetConfigValue("id", "id", cmd, int32(0)).(int32)
id := viper.GetString("id")

conn, err := util.GrpcForCommand(cmd)

Expand All @@ -61,7 +61,7 @@ mediator control plane.`,

func init() {
ruleTypeCmd.AddCommand(ruleType_deleteCmd)
ruleType_deleteCmd.Flags().Int32P("id", "i", 0, "id of rule type to delete")
ruleType_deleteCmd.Flags().StringP("id", "i", "", "id of rule type to delete")
err := ruleType_deleteCmd.MarkFlagRequired("id")
util.ExitNicelyOnError(err, "Error marking flag as required")
}
4 changes: 2 additions & 2 deletions cmd/cli/app/rule_type/rule_type_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ mediator control plane.`,
ctx, cancel := util.GetAppContext()
defer cancel()

id := viper.GetInt32("id")
id := viper.GetString("id")

rtype, err := client.GetRuleTypeById(ctx, &pb.GetRuleTypeByIdRequest{
Context: &pb.Context{
Expand Down Expand Up @@ -89,7 +89,7 @@ mediator control plane.`,

func init() {
ruleTypeCmd.AddCommand(ruleType_getCmd)
ruleType_getCmd.Flags().Int32P("id", "i", 0, "ID for the policy to query")
ruleType_getCmd.Flags().StringP("id", "i", "", "ID for the policy to query")
ruleType_getCmd.Flags().StringP("output", "o", app.Table, "Output format (json, yaml or table)")
ruleType_getCmd.Flags().StringP("provider", "p", "github", "Provider for the policy")
// TODO set up group if specified
Expand Down
4 changes: 1 addition & 3 deletions cmd/cli/app/rule_type/table_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package rule_type

import (
"fmt"

"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"

Expand All @@ -43,7 +41,7 @@ func renderRuleTypeTable(
row := []string{
rt.Context.Provider,
*rt.Context.Group,
fmt.Sprintf("%d", *rt.Id),
*rt.Id,
rt.Name,
rt.Description,
}
Expand Down
Loading

0 comments on commit 304bbf3

Please sign in to comment.