Skip to content
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b27a676
Added `scanner create` command.
muaz-32 Jun 4, 2024
6145a99
Improved the `scanner create` command. It tests the connection before…
muaz-32 Jun 7, 2024
011fbee
Update pkg/api/scanner_handler.go
muaz-32 Jun 7, 2024
1164f30
Added the code review suggestions.
muaz-32 Jun 7, 2024
2c319c5
Added the `view` command for scanner.
muaz-32 Jun 8, 2024
0ca2656
Added the `metadata` command for scanner.
muaz-32 Jun 8, 2024
a8ea7e8
Added the `set-default` command for scanner.
muaz-32 Jun 8, 2024
e8e2199
Added the `delete` command for scanner.
muaz-32 Jun 8, 2024
136a8d5
Added the `update` command for scanner and fixed the `create` command…
muaz-32 Jun 8, 2024
eececca
Added the `ping` option while creating scanner to mimic the web UI in…
muaz-32 Jun 8, 2024
7746681
Merge branch 'main' into scanner-cmd
rizul2108 Apr 12, 2025
110474f
add header and gofmt
rizul2108 Apr 12, 2025
65c85ef
add docs
rizul2108 Apr 12, 2025
927ac5b
lint fixes
rizul2108 Apr 12, 2025
ae372f0
lint fixes
rizul2108 Apr 12, 2025
ac5c0d7
update list cmd
rizul2108 Apr 12, 2025
63f05f1
add header
rizul2108 Apr 12, 2025
83445a5
improve ui
rizul2108 Apr 12, 2025
a904e2c
fix lint issues
rizul2108 Apr 12, 2025
c6492d6
Update pkg/views/scanner/list/view.go
rizul2108 Apr 13, 2025
0b47ef2
Update cmd/harbor/root/cmd.go
rizul2108 Apr 13, 2025
9b3cf54
fix: implement changes and apply lint fixes
rizul2108 Apr 14, 2025
1dddf42
Merge branch 'main' into scanner-cmd
rizul2108 Apr 18, 2025
087781c
Merge branch 'main' into scanner-cmd
rizul2108 May 6, 2025
3e15892
implement changes and update docs
rizul2108 May 6, 2025
c4b5543
small fix
rizul2108 May 6, 2025
5a4e255
Merge branch 'main' into scanner-cmd
rizul2108 May 12, 2025
d5b1ecc
Merge branch 'main' into scanner-cmd
bupd May 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/goharbor/harbor-cli/cmd/harbor/root/project"
"github.com/goharbor/harbor-cli/cmd/harbor/root/registry"
repositry "github.com/goharbor/harbor-cli/cmd/harbor/root/repository"
"github.com/goharbor/harbor-cli/cmd/harbor/root/scanner"
"github.com/goharbor/harbor-cli/cmd/harbor/root/schedule"
"github.com/goharbor/harbor-cli/cmd/harbor/root/user"
"github.com/goharbor/harbor-cli/pkg/utils"
Expand Down Expand Up @@ -96,6 +97,7 @@ harbor help
repositry.Repository(),
user.User(),
artifact.Artifact(),
scanner.Scanner(),
HealthCommand(),
schedule.Schedule(),
labels.Labels(),
Expand Down
35 changes: 35 additions & 0 deletions cmd/harbor/root/scanner/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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 scanner

import "github.com/spf13/cobra"

func Scanner() *cobra.Command {
cmd := &cobra.Command{
Use: "scanner",
Short: "scanner commands",
}

cmd.AddCommand(
CreateScannerCommand(),
ListScannerCommand(),
ViewCommand(),
MetadataCommand(),
SetDefaultCommand(),
DeleteCommand(),
UpdateCommand(),
)

return cmd
}
62 changes: 62 additions & 0 deletions cmd/harbor/root/scanner/create.go
Original file line number Diff line number Diff line change
@@ -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 scanner

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/views/scanner/create"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func CreateScannerCommand() *cobra.Command {
var opts create.CreateView
var ping bool

cmd := &cobra.Command{
Use: "create",
Short: "Create a scanner",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if opts.Name == "" || opts.Description == "" || opts.Auth == "" || opts.AccessCredential == "" || opts.URL == "" {
Comment thread
rizul2108 marked this conversation as resolved.
Outdated
create.CreateScannerView(&opts)
}

if ping {
err := api.PingScanner(opts)
if err != nil {
log.Errorf("failed to ping the scanner adapter: %v", err)
}
} else {
err := api.CreateScanner(opts)
if err != nil {
log.Errorf("failed to create scanner: %v", err.Error())
}
}
},
}

flags := cmd.Flags()
flags.StringVarP(&opts.Name, "name", "", "", "Name of the scanner")
flags.StringVarP(&opts.Description, "des", "", "", "Description of the scanner")
flags.StringVarP(&opts.Auth, "auth", "", "", "Authentication approach of the scanner [None|Basic|Bearer|X-ScannerAdapter-API-Key]")
flags.StringVarP(&opts.AccessCredential, "cred", "", "", "HTTP Authorization header value sent with each request to the Scanner Adapter API")
flags.StringVarP(&opts.URL, "url", "", "", "Base URL of the scanner adapter")
flags.BoolVarP(&opts.Disabled, "disable", "", false, "Indicate whether the registration is enabled or not")
flags.BoolVarP(&opts.SkipCertVerify, "skip", "", false, "Indicate if skip the certificate verification when sending HTTP requests")
flags.BoolVarP(&opts.UseInternalAddr, "internal", "", false, "Indicate whether use internal registry addr for the scanner to pull content or not")
flags.BoolVarP(&ping, "ping", "", false, "Ping the scanner adapter without creating it.")

return cmd
}
45 changes: 45 additions & 0 deletions cmd/harbor/root/scanner/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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 scanner

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/prompt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func DeleteCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "delete scanner",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error
var registrationID string
if len(args) > 0 {
registrationID = args[0]
} else {
registrationID = prompt.GetScannerIdFromUser()
}
Comment thread
rizul2108 marked this conversation as resolved.
Outdated
err = api.DeleteScanner(registrationID)
if err != nil {
log.Errorf("failed to delete scanner: %v", err)
} else {
log.Infof("scanner deleted successfully")
}
},
}
return cmd
}
58 changes: 58 additions & 0 deletions cmd/harbor/root/scanner/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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 scanner

import (
"fmt"

"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/utils"
list "github.com/goharbor/harbor-cli/pkg/views/scanner/list"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func ListScannerCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List scanners",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
scannersResp, err := api.ListScanners()
if err != nil {
return fmt.Errorf("failed to list scanners: %v", err)
}

scanners := scannersResp.Payload
if len(scanners) == 0 {
log.Info("No scanners found")
return nil
}

formatFlag := viper.GetString("output-format")
if formatFlag != "" {
err = utils.PrintFormat(scanners, formatFlag)
if err != nil {
return err
}
} else {
list.ListScanners(scanners)
}

return nil
},
}
return cmd
}
60 changes: 60 additions & 0 deletions cmd/harbor/root/scanner/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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 scanner

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/scanner/metadata"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func MetadataCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "metadata [scanner-id]",
Short: "Get scanner metadata by ID",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var registrationID string
if len(args) > 0 {
registrationID = args[0]
} else {
registrationID = prompt.GetScannerIdFromUser()
}

meta, err := api.GetScannerMetadata(registrationID)
if err != nil {
return fmt.Errorf("failed to get scanner metadata: %v", err)
}

formatFlag := viper.GetString("output-format")
if formatFlag != "" {
err = utils.PrintFormat(meta, formatFlag)
if err != nil {
return err
Comment thread
rizul2108 marked this conversation as resolved.
}
} else {
metadata.DisplayScannerMetadata(meta.Payload)
Comment thread
rizul2108 marked this conversation as resolved.
}

return nil
},
}

return cmd
}
44 changes: 44 additions & 0 deletions cmd/harbor/root/scanner/set_default.go
Original file line number Diff line number Diff line change
@@ -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 scanner

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/prompt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func SetDefaultCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "set-default",
Short: "set default scanner",
Aliases: []string{"sd"},
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error
var registrationID string
if len(args) > 0 {
registrationID = args[0]
} else {
registrationID = prompt.GetScannerIdFromUser()
}
Comment thread
rizul2108 marked this conversation as resolved.
err = api.SetDefaultScanner(registrationID)
if err != nil {
log.Errorf("failed to set default scanner: %v", err)
}
},
}
return cmd
}
66 changes: 66 additions & 0 deletions cmd/harbor/root/scanner/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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 scanner

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/prompt"
"github.com/goharbor/harbor-cli/pkg/views/scanner/create"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func UpdateCommand() *cobra.Command {
Comment thread
rizul2108 marked this conversation as resolved.
var opts create.CreateView

cmd := &cobra.Command{
Use: "update",
Short: "update scanner",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error
var registrationID string

if len(args) > 0 {
registrationID = args[0]
} else {
registrationID = prompt.GetScannerIdFromUser()
}

if opts.Name == "" || opts.Description == "" || opts.Auth == "" || opts.AccessCredential == "" || opts.URL == "" {
create.CreateScannerView(&opts)
}

err = api.UpdateScanner(registrationID, opts)

if err != nil {
log.Errorf("failed to update scanner: %v", err)
} else {
log.Infof("scanner updated successfully")
}
},
}

flags := cmd.Flags()
flags.StringVarP(&opts.Name, "name", "", "", "Name of the scanner")
flags.StringVarP(&opts.Description, "des", "", "", "Description of the scanner")
flags.StringVarP(&opts.Auth, "auth", "", "", "Authentication approach of the scanner [None|Basic|Bearer|X-ScannerAdapter-API-Key]")
flags.StringVarP(&opts.AccessCredential, "cred", "", "", "HTTP Authorization header value sent with each request to the Scanner Adapter API")
flags.StringVarP(&opts.URL, "url", "", "", "Base URL of the scanner adapter")
flags.BoolVarP(&opts.Disabled, "disable", "", false, "Indicate whether the registration is enabled or not")
flags.BoolVarP(&opts.SkipCertVerify, "skip", "", false, "Indicate if skip the certificate verification when sending HTTP requests")
flags.BoolVarP(&opts.UseInternalAddr, "internal", "", false, "Indicate whether use internal registry addr for the scanner to pull content or not")

return cmd
}
Loading