-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add cli commands, fix bugs in viewer
- Loading branch information
Showing
26 changed files
with
824 additions
and
477 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
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,79 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"os/user" | ||
"path/filepath" | ||
|
||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
var loginCmd = &cobra.Command{ | ||
Use: "login", | ||
Short: "Authenticate to the cli with your API Key", | ||
Run: doLoginCmd, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(loginCmd) | ||
} | ||
|
||
// doLoginCmd implements the login logic | ||
func doLoginCmd(cmd *cobra.Command, args []string) { | ||
var apiKey string | ||
|
||
fmt.Print("Enter your API key: ") | ||
_, err := fmt.Scanln(&apiKey) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error reading API key: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Get the user's home directory | ||
usr, err := user.Current() | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Cannot find the user home directory: %v\n", err) | ||
os.Exit(1) | ||
} | ||
configPath := filepath.Join(usr.HomeDir, ".warpdiverc") | ||
|
||
// Check if the WARPDIVE_ENDPOINT environment variable is set and is a valid URL | ||
endpoint := os.Getenv("WARPDIVE_ENDPOINT") | ||
if endpoint == "" { | ||
endpoint = "https://www.warpdive.xyz" // Default URL | ||
fmt.Println("Using default API endpoint.") | ||
} else { | ||
_, err := url.ParseRequestURI(endpoint) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Invalid URL in WARPDIVE_ENDPOINT: %v\n", err) | ||
os.Exit(1) | ||
} | ||
fmt.Printf("Using custom API endpoint: %s\n", endpoint) | ||
} | ||
|
||
// Prepare the API key data in YAML format using the endpoint | ||
data := map[string]interface{}{ | ||
endpoint: map[string]interface{}{ | ||
"api_key": map[string]string{ | ||
"value": apiKey, | ||
}, | ||
}, | ||
} | ||
fileData, err := yaml.Marshal(data) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error marshaling API key data: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Write the data to the .warpdiverc file | ||
err = os.WriteFile(configPath, fileData, 0600) // rw------- permissions | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error writing API key to file: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Println("API Key saved successfully to ~/.warpdiverc.") | ||
} |
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,88 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/gvkhna/warpdive/dive" | ||
"github.com/gvkhna/warpdive/runtime" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var pushCmd = &cobra.Command{ | ||
Use: "push [IMAGE ID]", | ||
Short: "Pushes a container image to to warpdive.xyz", | ||
Args: cobra.ExactArgs(1), // Expect exactly one argument: the docker image/tag id | ||
// DisableFlagParsing: true, | ||
Run: doPushCmd, | ||
} | ||
|
||
// var outputFilePath string | ||
|
||
func init() { | ||
rootCmd.AddCommand(pushCmd) | ||
// exportCmd.Flags().StringVarP(&outputFilePath, "output", "o", "", "Optional: Specify the output file path or directory to place the .warpdive file") | ||
// rootCmd.PersistentFlags().String("source", "docker", "The container engine to fetch the image from. Allowed values: "+strings.Join(dive.ImageSources, ", ")) | ||
|
||
// exportCmd.Flags().StringVarP(&outputFilePath, "output", "o", "", "Optional: Specify the output file path or directory to place the .warpdive file") | ||
} | ||
|
||
// doExportCmd implements the steps taken for the export command | ||
func doPushCmd(cmd *cobra.Command, args []string) { | ||
// initLogging() | ||
// imageID := args[0] | ||
|
||
// engine := viper.GetString("source") | ||
// engine := viper.GetString("container-engine") | ||
// fmt.Printf("Using engine %s to export image %s to file %s\n", engine, imageID, outputFilePath) | ||
|
||
// fmt.Printf("Exporting image to file %s\n", outputFilePath) | ||
|
||
userImage := args[0] | ||
if userImage == "" { | ||
fmt.Println("No image argument given") | ||
os.Exit(1) | ||
} | ||
|
||
// initLogging() | ||
|
||
// isCi, ciConfig, err := configureCi() | ||
|
||
// if err != nil { | ||
// fmt.Printf("ci configuration error: %v\n", err) | ||
// os.Exit(1) | ||
// } | ||
|
||
var sourceType dive.ImageSource | ||
var imageStr string | ||
|
||
sourceType, imageStr = dive.DeriveImageSource(userImage) | ||
|
||
if sourceType == dive.SourceUnknown { | ||
sourceStr := viper.GetString("source") | ||
sourceType = dive.ParseImageSource(sourceStr) | ||
if sourceType == dive.SourceUnknown { | ||
fmt.Printf("unable to determine image source: %v\n", sourceStr) | ||
os.Exit(1) | ||
} | ||
|
||
imageStr = userImage | ||
} | ||
|
||
// ignoreErrors, err := cmd.PersistentFlags().GetBool("ignore-errors") | ||
// if err != nil { | ||
// logrus.Error("unable to get 'ignore-errors' option:", err) | ||
// } | ||
|
||
runtime.Run(runtime.Options{ | ||
// Ci: isCi, | ||
Image: imageStr, | ||
Engine: defaultSource, | ||
Source: dive.ParseImageSource(defaultSource), | ||
// Source: dive.ParseImageSource(engine), | ||
PushArgs: args, | ||
// ExportFile: outputFilePath, | ||
// CiConfig: ciConfig, | ||
}) | ||
} |
Oops, something went wrong.