-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathartifact_list.go
More file actions
104 lines (88 loc) · 2.88 KB
/
Copy pathartifact_list.go
File metadata and controls
104 lines (88 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// SPDX-FileCopyrightText: Copyright 2023 The Minder Authors
// SPDX-License-Identifier: Apache-2.0
package artifact
import (
"fmt"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/mindersec/minder/cmd/cli/app"
"github.com/mindersec/minder/internal/util"
"github.com/mindersec/minder/internal/util/cli"
"github.com/mindersec/minder/internal/util/cli/table"
"github.com/mindersec/minder/internal/util/cli/table/layouts"
minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1"
)
var listCmd = &cobra.Command{
Use: "list",
Short: "List artifacts from a provider",
Long: `The artifact list subcommand will list artifacts from a provider.`,
PreRunE: func(cmd *cobra.Command, _ []string) error {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return fmt.Errorf("error binding flags: %w", err)
}
format := viper.GetString("output")
if !app.IsOutputFormatSupported(format) {
return cli.MessageAndError(fmt.Sprintf("Output format %s not supported", format), fmt.Errorf("invalid argument"))
}
return nil
},
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
client, closer, err := cli.GetCLIClient(cmd, minderv1.NewArtifactServiceClient)
if err != nil {
return err
}
defer closer()
provider := viper.GetString("provider")
project := viper.GetString("project")
format := viper.GetString("output")
fromFilter := viper.GetString("from")
cmd.SilenceUsage = true
artifactList, err := client.ListArtifacts(ctx, &minderv1.ListArtifactsRequest{
Context: &minderv1.Context{Provider: &provider, Project: &project},
From: fromFilter,
})
if err != nil {
return cli.MessageAndError("Couldn't list artifacts", err)
}
switch format {
case app.Table:
t := table.New(table.Simple, layouts.Default, cmd.OutOrStdout(),
[]string{"ID", "Type", "Owner", "Name", "Repository", "Visibility", "Creation date"})
for _, artifact := range artifactList.Results {
t.AddRow(
artifact.ArtifactPk,
artifact.Type,
artifact.GetOwner(),
artifact.GetName(),
artifact.Repository,
artifact.Visibility,
artifact.CreatedAt.AsTime().Format(time.RFC3339),
)
}
t.Render()
case app.JSON:
out, err := util.GetJsonFromProto(artifactList)
if err != nil {
return cli.MessageAndError("Error getting json from proto", err)
}
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
},
}
func init() {
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")
}