diff --git a/cmd/harbor/root/artifact/cmd.go b/cmd/harbor/root/artifact/cmd.go index 0a70203e..6141b681 100644 --- a/cmd/harbor/root/artifact/cmd.go +++ b/cmd/harbor/root/artifact/cmd.go @@ -15,6 +15,8 @@ package artifact import ( "github.com/goharbor/harbor-cli/cmd/harbor/root/artifact/label" + artifactscan "github.com/goharbor/harbor-cli/cmd/harbor/root/artifact/scan" + artifacttags "github.com/goharbor/harbor-cli/cmd/harbor/root/artifact/tags" "github.com/spf13/cobra" ) @@ -30,8 +32,8 @@ func Artifact() *cobra.Command { ListArtifactCommand(), ViewArtifactCommmand(), DeleteArtifactCommand(), - ScanArtifactCommand(), - ArtifactTagsCmd(), + artifactscan.ScanArtifactCommand(), + artifacttags.ArtifactTagsCmd(), label.LabelsArtifactCommmand(), ) diff --git a/cmd/harbor/root/artifact/scan.go b/cmd/harbor/root/artifact/scan.go deleted file mode 100644 index 4040e62f..00000000 --- a/cmd/harbor/root/artifact/scan.go +++ /dev/null @@ -1,107 +0,0 @@ -// Copyright Project Harbor Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -package artifact - -import ( - "fmt" - - "github.com/goharbor/harbor-cli/pkg/api" - "github.com/goharbor/harbor-cli/pkg/prompt" - "github.com/goharbor/harbor-cli/pkg/utils" - log "github.com/sirupsen/logrus" - "github.com/spf13/cobra" -) - -func ScanArtifactCommand() *cobra.Command { - cmd := &cobra.Command{ - Use: "scan", - Short: "Scan an artifact", - Long: `Scan an artifact in Harbor Repository`, - Example: `harbor artifact scan start //`, - } - - cmd.AddCommand( - StartScanArtifactCommand(), - StopScanArtifactCommand(), - // LogScanArtifactCommand(), - ) - - return cmd -} - -func StartScanArtifactCommand() *cobra.Command { - cmd := &cobra.Command{ - Use: "start", - Short: "Start a scan of an artifact", - Long: `Start a scan of an artifact in Harbor Repository`, - Example: `harbor artifact scan start //`, - RunE: func(cmd *cobra.Command, args []string) error { - var err error - var projectName, repoName, reference string - - if len(args) > 0 { - projectName, repoName, reference, err = utils.ParseProjectRepoReference(args[0]) - if err != nil { - return fmt.Errorf("failed to parse project/repo/reference: %v", err) - } - } else { - projectName, err = prompt.GetProjectNameFromUser() - if err != nil { - return fmt.Errorf("failed to get project name: %v", utils.ParseHarborErrorMsg(err)) - } - repoName = prompt.GetRepoNameFromUser(projectName) - reference = prompt.GetReferenceFromUser(repoName, projectName) - } - err = api.StartScanArtifact(projectName, repoName, reference) - if err != nil { - return fmt.Errorf("failed to start scan of artifact: %v", err) - } - return nil - }, - } - return cmd -} - -func StopScanArtifactCommand() *cobra.Command { - cmd := &cobra.Command{ - Use: "stop", - Short: "Stop a scan of an artifact", - Long: `Stop a scan of an artifact in Harbor Repository`, - Example: `harbor artifact scan stop //`, - Run: func(cmd *cobra.Command, args []string) { - var err error - var projectName, repoName, reference string - - if len(args) > 0 { - projectName, repoName, reference, err = utils.ParseProjectRepoReference(args[0]) - if err != nil { - log.Errorf("failed to parse project/repo/reference: %v", err) - } - } else { - projectName, err = prompt.GetProjectNameFromUser() - if err != nil { - log.Errorf("failed to get project name: %v", utils.ParseHarborErrorMsg(err)) - } - repoName = prompt.GetRepoNameFromUser(projectName) - reference = prompt.GetReferenceFromUser(repoName, projectName) - } - - err = api.StopScanArtifact(projectName, repoName, reference) - if err != nil { - log.Errorf("failed to stop scan of artifact: %v", err) - } - }, - } - return cmd -} diff --git a/cmd/harbor/root/artifact/scan/scan.go b/cmd/harbor/root/artifact/scan/scan.go new file mode 100644 index 00000000..949fa200 --- /dev/null +++ b/cmd/harbor/root/artifact/scan/scan.go @@ -0,0 +1,52 @@ +// Copyright Project Harbor Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package artifactscan + +import ( + "github.com/goharbor/harbor-cli/pkg/prompt" + "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/spf13/cobra" +) + +func ScanArtifactCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "scan", + Short: "Scan an artifact", + Long: `Scan an artifact in Harbor Repository`, + Example: `harbor artifact scan start //`, + } + + cmd.AddCommand( + StartScanArtifactCommand(), + StopScanArtifactCommand(), + // LogScanArtifactCommand(), + ) + + return cmd +} + +func parseArgs(args []string) (string, string, string, error) { + if len(args) > 0 { + return utils.ParseProjectRepoReference(args[0]) + } else { + projectName, err := prompt.GetProjectNameFromUser() + if err != nil { + return "", "", "", err + } + repoName := prompt.GetRepoNameFromUser(projectName) + reference := prompt.GetReferenceFromUser(repoName, projectName) + + return projectName, repoName, reference, nil + } +} diff --git a/cmd/harbor/root/artifact/scan/start.go b/cmd/harbor/root/artifact/scan/start.go new file mode 100644 index 00000000..a1aa732a --- /dev/null +++ b/cmd/harbor/root/artifact/scan/start.go @@ -0,0 +1,43 @@ +// Copyright Project Harbor Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package artifactscan + +import ( + "fmt" + + "github.com/goharbor/harbor-cli/pkg/api" + "github.com/spf13/cobra" +) + +func StartScanArtifactCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "start", + Short: "Start a scan of an artifact", + Long: `Start a scan of an artifact in Harbor Repository`, + Example: `harbor artifact scan start //`, + RunE: func(cmd *cobra.Command, args []string) error { + projectName, repoName, reference, err := parseArgs(args) + if err != nil { + return err + } + + err = api.StartScanArtifact(projectName, repoName, reference) + if err != nil { + return fmt.Errorf("failed to start scan of artifact: %v", err) + } + return nil + }, + } + return cmd +} diff --git a/cmd/harbor/root/artifact/scan/stop.go b/cmd/harbor/root/artifact/scan/stop.go new file mode 100644 index 00000000..9ec132c9 --- /dev/null +++ b/cmd/harbor/root/artifact/scan/stop.go @@ -0,0 +1,44 @@ +// Copyright Project Harbor Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package artifactscan + +import ( + "fmt" + + "github.com/goharbor/harbor-cli/pkg/api" + "github.com/spf13/cobra" +) + +func StopScanArtifactCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "stop", + Short: "Stop a scan of an artifact", + Long: `Stop a scan of an artifact in Harbor Repository`, + Example: `harbor artifact scan stop //`, + RunE: func(cmd *cobra.Command, args []string) error { + projectName, repoName, reference, err := parseArgs(args) + if err != nil { + return err + } + + err = api.StopScanArtifact(projectName, repoName, reference) + if err != nil { + return fmt.Errorf("failed to stop scan of artifact: %v", err) + } + + return nil + }, + } + return cmd +} diff --git a/cmd/harbor/root/artifact/tags.go b/cmd/harbor/root/artifact/tags.go deleted file mode 100644 index 4f9a60f5..00000000 --- a/cmd/harbor/root/artifact/tags.go +++ /dev/null @@ -1,160 +0,0 @@ -// Copyright Project Harbor Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -package artifact - -import ( - "github.com/goharbor/go-client/pkg/sdk/v2.0/client/artifact" - "github.com/goharbor/harbor-cli/pkg/api" - "github.com/goharbor/harbor-cli/pkg/prompt" - "github.com/goharbor/harbor-cli/pkg/utils" - "github.com/goharbor/harbor-cli/pkg/views/artifact/tags/create" - "github.com/goharbor/harbor-cli/pkg/views/artifact/tags/list" - log "github.com/sirupsen/logrus" - "github.com/spf13/cobra" - "github.com/spf13/viper" -) - -func ArtifactTagsCmd() *cobra.Command { - cmd := &cobra.Command{ - Use: "tags", - Short: "Manage tags of an artifact", - Example: ` harbor artifact tags list //`, - } - - cmd.AddCommand( - ListTagsCmd(), - DeleteTagsCmd(), - CreateTagsCmd(), - ) - - return cmd -} - -func CreateTagsCmd() *cobra.Command { - cmd := &cobra.Command{ - Use: "create", - Short: "Create a tag of an artifact", - Example: `harbor artifact tags create // `, - Run: func(cmd *cobra.Command, args []string) { - var err error - var projectName, repoName, reference string - var tagName string - if len(args) > 0 { - projectName, repoName, reference, err = utils.ParseProjectRepoReference(args[0]) - if err != nil { - log.Errorf("failed to parse project/repo/reference: %v", err) - } - tagName = args[1] - } else { - projectName, err = prompt.GetProjectNameFromUser() - if err != nil { - log.Errorf("failed to get project name: %v", utils.ParseHarborErrorMsg(err)) - } - repoName = prompt.GetRepoNameFromUser(projectName) - reference = prompt.GetReferenceFromUser(repoName, projectName) - create.CreateTagView(&tagName) - } - err = api.CreateTag(projectName, repoName, reference, tagName) - if err != nil { - log.Errorf("failed to create tag: %v", err) - } - }, - } - - return cmd -} - -func ListTagsCmd() *cobra.Command { - cmd := &cobra.Command{ - Use: "list", - Short: "List tags of an artifact", - Example: `harbor artifact tags list //`, - Run: func(cmd *cobra.Command, args []string) { - var err error - var tags *artifact.ListTagsOK - var projectName, repoName, reference string - - if len(args) > 0 { - projectName, repoName, reference, err = utils.ParseProjectRepoReference(args[0]) - if err != nil { - log.Errorf("failed to parse project/repo/reference: %v", err) - } - } else { - projectName, err = prompt.GetProjectNameFromUser() - if err != nil { - log.Errorf("failed to get project name: %v", utils.ParseHarborErrorMsg(err)) - } - repoName = prompt.GetRepoNameFromUser(projectName) - if repoName == "" { - return - } - reference = prompt.GetReferenceFromUser(repoName, projectName) - } - - tags, err = api.ListTags(projectName, repoName, reference) - - if err != nil { - log.Errorf("failed to list tags: %v", err) - return - } - - FormatFlag := viper.GetString("output-format") - if FormatFlag != "" { - err = utils.PrintFormat(tags, FormatFlag) - if err != nil { - log.Error(err) - return - } - } else { - list.ListTags(tags.Payload) - } - }, - } - - return cmd -} - -func DeleteTagsCmd() *cobra.Command { - cmd := &cobra.Command{ - Use: "delete", - Short: "Delete a tag of an artifact", - Example: `harbor artifact tags delete // `, - Run: func(cmd *cobra.Command, args []string) { - var err error - var projectName, repoName, reference string - var tagName string - if len(args) > 0 { - projectName, repoName, reference, err = utils.ParseProjectRepoReference(args[0]) - if err != nil { - log.Errorf("failed to parse project/repo/reference: %v", err) - } - tagName = args[1] - } else { - projectName, err = prompt.GetProjectNameFromUser() - if err != nil { - log.Errorf("failed to get project name: %v", utils.ParseHarborErrorMsg(err)) - } - repoName = prompt.GetRepoNameFromUser(projectName) - reference = prompt.GetReferenceFromUser(repoName, projectName) - tagName = prompt.GetTagFromUser(repoName, projectName, reference) - } - err = api.DeleteTag(projectName, repoName, reference, tagName) - if err != nil { - log.Errorf("failed to delete tag: %v", err) - } - }, - } - - return cmd -} diff --git a/cmd/harbor/root/artifact/tags/create.go b/cmd/harbor/root/artifact/tags/create.go new file mode 100644 index 00000000..9ebeb312 --- /dev/null +++ b/cmd/harbor/root/artifact/tags/create.go @@ -0,0 +1,63 @@ +// Copyright Project Harbor Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package artifacttags + +import ( + "fmt" + + "github.com/goharbor/harbor-cli/pkg/api" + "github.com/goharbor/harbor-cli/pkg/prompt" + "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/goharbor/harbor-cli/pkg/views/artifact/tags/create" + "github.com/spf13/cobra" +) + +func CreateTagsCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "create", + Short: "Create a tag of an artifact", + Example: `harbor artifact tags create // `, + RunE: func(cmd *cobra.Command, args []string) error { + var err error + var projectName, repoName, reference string + var tagName string + + if len(args) > 0 { + projectName, repoName, reference, err = utils.ParseProjectRepoReference(args[0]) + if err != nil { + return fmt.Errorf("failed to parse project/repo/reference: %v", err) + } + + tagName = args[1] + } else { + projectName, err = prompt.GetProjectNameFromUser() + if err != nil { + return fmt.Errorf("failed to get project name: %v", utils.ParseHarborErrorMsg(err)) + } + + repoName = prompt.GetRepoNameFromUser(projectName) + reference = prompt.GetReferenceFromUser(repoName, projectName) + create.CreateTagView(&tagName) + } + err = api.CreateTag(projectName, repoName, reference, tagName) + if err != nil { + return fmt.Errorf("failed to create tag: %v", err) + } + + return nil + }, + } + + return cmd +} diff --git a/cmd/harbor/root/artifact/tags/delete.go b/cmd/harbor/root/artifact/tags/delete.go new file mode 100644 index 00000000..8d6090af --- /dev/null +++ b/cmd/harbor/root/artifact/tags/delete.go @@ -0,0 +1,62 @@ +// Copyright Project Harbor Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package artifacttags + +import ( + "fmt" + + "github.com/goharbor/harbor-cli/pkg/api" + "github.com/goharbor/harbor-cli/pkg/prompt" + "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/spf13/cobra" +) + +func DeleteTagsCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "delete", + Short: "Delete a tag of an artifact", + Example: `harbor artifact tags delete // `, + RunE: func(cmd *cobra.Command, args []string) error { + var err error + var projectName, repoName, reference string + var tagName string + if len(args) > 0 { + projectName, repoName, reference, err = utils.ParseProjectRepoReference(args[0]) + if err != nil { + return fmt.Errorf("failed to parse project/repo/reference: %v", err) + } + + tagName = args[1] + } else { + projectName, err = prompt.GetProjectNameFromUser() + if err != nil { + return fmt.Errorf("failed to get project name: %v", utils.ParseHarborErrorMsg(err)) + } + + repoName = prompt.GetRepoNameFromUser(projectName) + reference = prompt.GetReferenceFromUser(repoName, projectName) + tagName = prompt.GetTagFromUser(repoName, projectName, reference) + } + + err = api.DeleteTag(projectName, repoName, reference, tagName) + if err != nil { + return fmt.Errorf("failed to delete tag: %v", err) + } + + return nil + }, + } + + return cmd +} diff --git a/cmd/harbor/root/artifact/tags/list.go b/cmd/harbor/root/artifact/tags/list.go new file mode 100644 index 00000000..9d057460 --- /dev/null +++ b/cmd/harbor/root/artifact/tags/list.go @@ -0,0 +1,77 @@ +// Copyright Project Harbor Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package artifacttags + +import ( + "fmt" + + "github.com/goharbor/go-client/pkg/sdk/v2.0/client/artifact" + "github.com/goharbor/harbor-cli/pkg/api" + "github.com/goharbor/harbor-cli/pkg/prompt" + "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/goharbor/harbor-cli/pkg/views/artifact/tags/list" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +func ListTagsCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "List tags of an artifact", + Example: `harbor artifact tags list //`, + RunE: func(cmd *cobra.Command, args []string) error { + var err error + var tags *artifact.ListTagsOK + var projectName, repoName, reference string + + if len(args) > 0 { + projectName, repoName, reference, err = utils.ParseProjectRepoReference(args[0]) + if err != nil { + return fmt.Errorf("failed to parse project/repo/reference: %v", err) + } + } else { + projectName, err = prompt.GetProjectNameFromUser() + if err != nil { + return fmt.Errorf("failed to get project name: %v", utils.ParseHarborErrorMsg(err)) + } + + repoName = prompt.GetRepoNameFromUser(projectName) + if repoName == "" { + return fmt.Errorf("invalid repository name provided") + } + reference = prompt.GetReferenceFromUser(repoName, projectName) + } + + tags, err = api.ListTags(projectName, repoName, reference) + if err != nil { + return fmt.Errorf("failed to list tags: %v", err) + } + + FormatFlag := viper.GetString("output-format") + if FormatFlag != "" { + err = utils.PrintFormat(tags, FormatFlag) + if err != nil { + return err + } + } else { + list.ListTags(tags.Payload) + } + + return nil + }, + } + + return cmd +} diff --git a/cmd/harbor/root/artifact/tags/tags.go b/cmd/harbor/root/artifact/tags/tags.go new file mode 100644 index 00000000..1433f1d4 --- /dev/null +++ b/cmd/harbor/root/artifact/tags/tags.go @@ -0,0 +1,34 @@ +// Copyright Project Harbor Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package artifacttags + +import ( + "github.com/spf13/cobra" +) + +func ArtifactTagsCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "tags", + Short: "Manage tags of an artifact", + Example: ` harbor artifact tags list //`, + } + + cmd.AddCommand( + ListTagsCmd(), + DeleteTagsCmd(), + CreateTagsCmd(), + ) + + return cmd +}