Skip to content

Commit

Permalink
Rebase latest artifact changes
Browse files Browse the repository at this point in the history
Signed-off-by: Radoslav Dimitrov <[email protected]>
  • Loading branch information
rdimitrov committed Dec 19, 2023
1 parent dd281fd commit 40f0230
Showing 1 changed file with 69 additions and 68 deletions.
137 changes: 69 additions & 68 deletions cmd/cli/app/artifact/artifact_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,94 +18,95 @@ package artifact
import (
"context"
"fmt"
"os"
"strings"
"time"

"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"

"github.com/stacklok/minder/cmd/cli/app"
"github.com/stacklok/minder/internal/util"
"github.com/stacklok/minder/internal/util/cli"
pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
"github.com/stacklok/minder/internal/util/cli/table"
minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
)

var artifact_listCmd = &cobra.Command{
var listCmd = &cobra.Command{
Use: "list",
Short: "List artifacts from a provider",
Long: `Artifact list will list artifacts from a provider`,
RunE: cli.GRPCClientWrapRunE(func(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientConn) error {
format := viper.GetString("output")
Long: `The artifact list subcommand will list artifacts from a provider.`,
RunE: cli.GRPCClientWrapRunE(listCommand),
}

provider := util.GetConfigValue(viper.GetViper(), "provider", "provider", cmd, "").(string)
projectID := viper.GetString("project-id")
// listCommand is the artifact list subcommand
func listCommand(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientConn) error {
client := minderv1.NewArtifactServiceClient(conn)

provider := viper.GetString("provider")
project := viper.GetString("project")
format := viper.GetString("output")

// Ensure provider is supported
if !app.IsProviderSupported(provider) {
return cli.MessageAndError(fmt.Sprintf("Provider %s is not supported yet", provider), fmt.Errorf("invalid argument"))
}

// Ensure the output format is supported
if !app.IsOutputFormatSupported(format) {
return cli.MessageAndError(fmt.Sprintf("Output format %s not supported", format), fmt.Errorf("invalid argument"))
}

artifactList, err := client.ListArtifacts(ctx, &minderv1.ListArtifactsRequest{
Context: &minderv1.Context{Provider: &provider, Project: &project},
// keep those until we decide to delete them from the payload and leave the context only
Provider: provider,
ProjectId: project,
From: viper.GetString("from"),
},
)

if err != nil {
return cli.MessageAndError("Couldn't list artifacts", err)
}

switch format {
case app.Table:
t := table.New(table.Simple, "", []string{"ID", "Type", "Owner", "Name", "Repository", "Visibility", "Creation date"})
for _, artifact := range artifactList.Results {
t.AddRow([]string{
artifact.ArtifactPk,
artifact.Type,
artifact.GetOwner(),
artifact.GetName(),
artifact.Repository,
artifact.Visibility,
artifact.CreatedAt.AsTime().Format(time.RFC3339),
})

switch format {
case "json":
case "yaml":
case "table":
case "":
default:
return fmt.Errorf("invalid output format: %s", format)
}

client := pb.NewArtifactServiceClient(conn)

artifacts, err := client.ListArtifacts(
ctx,
&pb.ListArtifactsRequest{
Provider: provider,
ProjectId: projectID,
From: viper.GetString("from"),
},
)

t.Render()
case app.JSON:
out, err := util.GetJsonFromProto(artifactList)
if err != nil {
return cli.MessageAndError(cmd, "Couldn't list artifacts", err)
return cli.MessageAndError("Error getting json from proto", err)
}

switch format {
case "", "table":
table := tablewriter.NewWriter(os.Stdout)

table.SetHeader([]string{"ID", "Type", "Owner", "Name", "Repository", "Visibility", "Creation date"})

for _, artifact_item := range artifacts.Results {
table.Append([]string{
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),
})
}

table.Render()
case "json":
out, err := util.GetJsonFromProto(artifacts)
if err != nil {
return cli.MessageAndError(cmd, "Error getting json from proto", err)
}
cmd.Println(out)
case "yaml":
out, err := util.GetYamlFromProto(artifacts)
if err != nil {
return cli.MessageAndError(cmd, "Error getting yaml from proto", err)
}
cmd.Println(out)
cmd.Println(out)
case app.YAML:
out, err := util.GetYamlFromProto(artifactList)
if err != nil {
return cli.MessageAndError("Error getting yaml from proto", err)
}
cmd.Println(out)
}

return nil
}),
return nil
}

func init() {
ArtifactCmd.AddCommand(artifact_listCmd)
artifact_listCmd.Flags().StringP("output", "f", "", "Output format (json or yaml)")
artifact_listCmd.Flags().StringP("provider", "p", "github", "Name for the provider to enroll")
artifact_listCmd.Flags().StringP("project-id", "g", "", "ID of the project for repo registration")
artifact_listCmd.Flags().String("from", "", "Filter artifacts from a source, example: from=repository=owner/repo")
ArtifactCmd.AddCommand(listCmd)
// Flags
listCmd.Flags().StringP("output", "o", app.Table,
fmt.Sprintf("Output format (one of %s)", strings.Join(app.SupportedOutputFormats(), ",")))
listCmd.Flags().String("from", "", "Filter artifacts from a source, example: from=repository=owner/repo")
}

0 comments on commit 40f0230

Please sign in to comment.