-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
390 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package login | ||
|
||
import ( | ||
"fmt" | ||
"github.com/charmbracelet/huh" | ||
"github.com/charmbracelet/huh/spinner" | ||
"github.com/spf13/cobra" | ||
"github.com/vulncheck-oss/cli/pkg/config" | ||
"github.com/vulncheck-oss/cli/pkg/session" | ||
"github.com/vulncheck-oss/cli/pkg/ui" | ||
"github.com/vulncheck-oss/sdk" | ||
) | ||
|
||
func chooseAuthMethod() (string, error) { | ||
|
||
var choice string | ||
form := huh.NewForm( | ||
huh.NewGroup( | ||
huh.NewSelect[string](). | ||
Title("Select an authentication method"). | ||
Options( | ||
huh.NewOption("Login with a web browser", "web"), | ||
huh.NewOption("Paste an authentication token", "token"), | ||
).Value(&choice), | ||
), | ||
) | ||
|
||
err := form.Run() | ||
if err != nil { | ||
return "", ui.Error("Failed to select authentication method: %v", err) | ||
} | ||
|
||
return choice, nil | ||
} | ||
|
||
func existingToken() error { | ||
logoutChoice := true | ||
confirm := huh.NewForm(huh.NewGroup(huh.NewConfirm(). | ||
Title("You currently have a token saved. Do you want to invalidate it first?"). | ||
Affirmative("Yes"). | ||
Negative("No"). | ||
Value(&logoutChoice))).WithTheme(huh.ThemeDracula()) | ||
confirm.Run() | ||
|
||
if logoutChoice { | ||
if _, err := session.InvalidateToken(config.Token()); err != nil { | ||
if err := config.RemoveToken(); err != nil { | ||
return ui.Error("Failed to remove token from config") | ||
} | ||
ui.Info("Token was not valid, removing from config") | ||
} else { | ||
if err := config.RemoveToken(); err != nil { | ||
return ui.Error("Failed to remove token from config") | ||
} | ||
ui.Success("Token invalidated successfully") | ||
} | ||
} else { | ||
return nil | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func cmdToken(cmd *cobra.Command, args []string) error { | ||
|
||
var token string | ||
|
||
input := huh. | ||
NewInput(). | ||
Title("Enter your authentication token"). | ||
Password(true). | ||
Placeholder("vulncheck_******************"). | ||
Value(&token) | ||
|
||
if err := input.Run(); err != nil { | ||
return ui.Error("Token verification failed: %v", err) | ||
} | ||
|
||
if !config.ValidToken(token) { | ||
return ui.Error("Invalid token specified") | ||
} | ||
|
||
return SaveToken(token) | ||
} | ||
|
||
func SaveToken(token string) error { | ||
|
||
var res *sdk.UserResponse | ||
var err error | ||
|
||
_ = spinner.New(). | ||
Style(ui.Pantone). | ||
Title(" Verifying token...").Action(func() { | ||
res, err = session.CheckToken(token) | ||
}).Run() | ||
|
||
if err != nil { | ||
return ui.Error("Token verification failed: %v", err) | ||
} | ||
if err := config.SaveToken(token); err != nil { | ||
return ui.Error("Failed to save token: %v", err) | ||
} | ||
ui.Success(fmt.Sprintf("Authenticated as %s (%s)", res.Data.Name, res.Data.Email)) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package index | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/vulncheck-oss/cli/pkg/config" | ||
"github.com/vulncheck-oss/cli/pkg/environment" | ||
"github.com/vulncheck-oss/cli/pkg/ui" | ||
"github.com/vulncheck-oss/sdk" | ||
) | ||
|
||
type indexOptions struct { | ||
Json bool | ||
Browse bool | ||
} | ||
|
||
func Command() *cobra.Command { | ||
|
||
opts := &indexOptions{} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "index <index>", | ||
Short: "", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) != 1 { | ||
return ui.Error("index name is required") | ||
} | ||
response, err := sdk.Connect(environment.Env.API, config.Token()).GetIndex(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if opts.Json { | ||
ui.Json(response.GetData()) | ||
return nil | ||
} | ||
|
||
if opts.Browse { | ||
ui.Viewport(args[0], response.GetData()) | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().BoolVar(&opts.Json, "json", false, "Output as JSON") | ||
cmd.Flags().BoolVar(&opts.Browse, "browse", false, "Browse the index in a pager") | ||
|
||
return cmd | ||
} |
Oops, something went wrong.