diff --git a/.DurpCLI.yaml b/.DurpCLI.yaml new file mode 100644 index 0000000..930b4e0 --- /dev/null +++ b/.DurpCLI.yaml @@ -0,0 +1,6 @@ +auth: + clientid: "" + granttype: client_credentials + password: "" + url: https://authentik.durp.info/application/o/token/ + username: "" diff --git a/GoCLI.backup.yaml b/GoCLI.backup.yaml deleted file mode 100644 index 3f4ada8..0000000 --- a/GoCLI.backup.yaml +++ /dev/null @@ -1 +0,0 @@ -port: "8080" diff --git a/GoCLI.yaml b/GoCLI.yaml deleted file mode 100644 index 25a9abb..0000000 --- a/GoCLI.yaml +++ /dev/null @@ -1,4 +0,0 @@ -cmd: - info: - diskUsage: - defaultDirectory: "/" \ No newline at end of file diff --git a/LICENSE b/LICENSE deleted file mode 100644 index e69de29..0000000 diff --git a/README.md b/README.md index 79feaeb..e69de29 100644 --- a/README.md +++ b/README.md @@ -1 +0,0 @@ -![toolboxdesign](./img/design.png) diff --git a/cmd/auth/auth.go b/cmd/auth/auth.go new file mode 100644 index 0000000..96fc9aa --- /dev/null +++ b/cmd/auth/auth.go @@ -0,0 +1,18 @@ +package auth + +import ( + "github.com/spf13/cobra" +) + +var AuthCmd = &cobra.Command{ + Use: "auth", + Short: "All things Authorization", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { + cmd.Help() + }, +} + +func init() { + +} diff --git a/cmd/auth/generateToken.go b/cmd/auth/generateToken.go new file mode 100644 index 0000000..0894b6e --- /dev/null +++ b/cmd/auth/generateToken.go @@ -0,0 +1,104 @@ +package auth + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strings" + + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +type accessTokenResponse struct { + AccessToken string `json:"access_token"` + TokenType string `json:"token_type"` + ExpiresIn int `json:"expires_in"` + IDToken string `json:"id_token"` +} + +var ( + clientID string + grantType string + url string + username string + password string +) + +var generateTokenCmd = &cobra.Command{ + Use: "generateToken", + Short: "Prints disk usage of the current directory", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { + + if clientID == "" { + clientID = viper.GetViper().GetString("auth.clientID") + } + if grantType == "" { + grantType = viper.GetViper().GetString("auth.grantType") + } + if url == "" { + url = viper.GetViper().GetString("auth.url") + } + if username == "" { + username = viper.GetViper().GetString("auth.username") + } + if password == "" { + password = viper.GetViper().GetString("auth.password") + } + + generateToken(clientID, grantType, url, username, password) + }, +} + +func init() { + generateTokenCmd.Flags().StringVarP(&clientID, "clientID", "c", "", "The ClientID") + generateTokenCmd.Flags().StringVarP(&grantType, "grantType", "g", "client_credentials", "The Grant Type") + generateTokenCmd.Flags().StringVarP(&url, "url", "", "", "Token URL") + generateTokenCmd.Flags().StringVarP(&username, "username", "u", "", "username") + generateTokenCmd.Flags().StringVarP(&password, "password", "p", "", "password") + + AuthCmd.AddCommand(generateTokenCmd) +} + +func generateToken(clientID string, grantType string, url string, username string, password string) { + + formData := fmt.Sprintf("grant_type=%s&client_id=%s&username=%s&password=%s", + grantType, clientID, username, password) + + client := &http.Client{} + + req, err := http.NewRequest("POST", url, strings.NewReader(formData)) + if err != nil { + fmt.Println("Error creating request:", err) + return + } + + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + resp, err := client.Do(req) + if err != nil { + fmt.Println("Error sending request:", err) + return + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + fmt.Println("Error reading response:", err) + return + } + + var response accessTokenResponse + err = json.Unmarshal(body, &response) + if err != nil { + fmt.Println("Error parsing response:", err) + return + } + + fmt.Println("Access_Token:", response.AccessToken) + fmt.Println("Token_Type:", response.TokenType) + fmt.Println("Expires_In:", response.ExpiresIn) + fmt.Println("ID_Token:", response.IDToken) +} diff --git a/cmd/cfg/cfg.go b/cmd/cfg/cfg.go new file mode 100644 index 0000000..e429a02 --- /dev/null +++ b/cmd/cfg/cfg.go @@ -0,0 +1,18 @@ +package cfg + +import ( + "github.com/spf13/cobra" +) + +var Cfgcmd = &cobra.Command{ + Use: "cfg", + Short: "All things Authorization", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { + cmd.Help() + }, +} + +func init() { + +} diff --git a/cmd/cfg/newconfig.go b/cmd/cfg/newconfig.go new file mode 100644 index 0000000..64f498c --- /dev/null +++ b/cmd/cfg/newconfig.go @@ -0,0 +1,26 @@ +package cfg + +import ( + "fmt" + + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var newcfgcmd = &cobra.Command{ + Use: "newconfig", + Short: "Generates a config file using current config", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { + err := viper.WriteConfigAs(".DurpCLI.yaml") + if err != nil { + fmt.Println(err) + } + + }, +} + +func init() { + + Cfgcmd.AddCommand(newcfgcmd) +} diff --git a/cmd/info/diskUsage.go b/cmd/info/diskUsage.go deleted file mode 100644 index d0952d3..0000000 --- a/cmd/info/diskUsage.go +++ /dev/null @@ -1,44 +0,0 @@ -/* -Copyright © 2022 NAME HERE - -*/ -package info - -import ( - "fmt" - - "github.com/ricochet2200/go-disk-usage/du" - "github.com/spf13/cobra" - "github.com/spf13/viper" -) - -// diskUsageCmd represents the diskUsage command -var diskUsageCmd = &cobra.Command{ - Use: "diskUsage", - Short: "Prints disk usage of the current directory", - Long: ``, - Run: func(cmd *cobra.Command, args []string) { - - defaultDirectory := "." - if dir := viper.GetViper().GetString("cmd.info.diskUsage.defaultDirectory"); dir != "" { - defaultDirectory = dir - } - usage := du.NewDiskUsage(defaultDirectory) - - fmt.Printf("Free disk space:%d in directory %s\n", usage.Free(), defaultDirectory) - }, -} - -func init() { - InfoCmd.AddCommand(diskUsageCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // diskUsageCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // diskUsageCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") -} diff --git a/cmd/info/info.go b/cmd/info/info.go deleted file mode 100644 index 96fb39c..0000000 --- a/cmd/info/info.go +++ /dev/null @@ -1,31 +0,0 @@ -/* -Copyright © 2022 NAME HERE - -*/ -package info - -import ( - "github.com/spf13/cobra" -) - -// infoCmd represents the info command -var InfoCmd = &cobra.Command{ - Use: "info", - Short: "All things information", - Long: ``, - Run: func(cmd *cobra.Command, args []string) { - cmd.Help() - }, -} - -func init() { - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // infoCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // infoCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") -} diff --git a/cmd/net/net.go b/cmd/net/net.go index b8e939b..1225f57 100644 --- a/cmd/net/net.go +++ b/cmd/net/net.go @@ -1,14 +1,9 @@ -/* -Copyright © 2022 NAME HERE - -*/ package net import ( "github.com/spf13/cobra" ) -// NetCmd represents the net command var NetCmd = &cobra.Command{ Use: "net", Short: "Net is a palette that contains network based commands", @@ -20,13 +15,4 @@ var NetCmd = &cobra.Command{ func init() { - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // netCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // netCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/cmd/net/ping.go b/cmd/net/ping.go index 7164045..2be09dd 100644 --- a/cmd/net/ping.go +++ b/cmd/net/ping.go @@ -1,7 +1,3 @@ -/* -Copyright © 2022 NAME HERE - -*/ package net import ( @@ -14,8 +10,7 @@ import ( var ( urlPath string - // Logic - client = http.Client{ + client = http.Client{ Timeout: time.Second * 2, } ) @@ -34,7 +29,6 @@ func ping(domain string) (int, error) { return resp.StatusCode, nil } -// pingCmd represents the ping command var pingCmd = &cobra.Command{ Use: "ping", Short: "This pings a remote URL and returns the response", @@ -57,13 +51,5 @@ func init() { fmt.Println(err) } - // Here you will define your flags and configuration settings. NetCmd.AddCommand(pingCmd) - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // pingCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // pingCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/cmd/root.go b/cmd/root.go index 930c429..d2a03f9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,38 +1,24 @@ -/* -Copyright © 2022 NAME HERE - -*/ package cmd import ( "fmt" "os" - "github.com/DeveloperDurp/GoCLI/cmd/info" - "github.com/DeveloperDurp/GoCLI/cmd/net" "github.com/spf13/cobra" "github.com/spf13/viper" + "gitlab.com/DeveloperDurp/DurpCLI/cmd/auth" + "gitlab.com/DeveloperDurp/DurpCLI/cmd/cfg" + "gitlab.com/DeveloperDurp/DurpCLI/cmd/net" ) var cfgFile string -// rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ - Use: "GoCLI", - Short: "A brief description of your application", - Long: `A longer description that spans multiple lines and likely contains -examples and usage of using your application. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, - // Uncomment the following line if your bare application - // has an action associated with it: - // Run: func(cmd *cobra.Command, args []string) { }, + Use: "DurpCLI", + Short: "CLI Tool made for Durp", + Long: ``, } -// Execute adds all child commands to the root command and sets flags appropriately. -// This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { err := rootCmd.Execute() if err != nil { @@ -41,7 +27,11 @@ func Execute() { } func setDefaults() { - viper.SetDefault("port", "8080") + viper.SetDefault("auth.url", "https://authentik.durp.info/application/o/token/") + viper.SetDefault("auth.grantType", "client_credentials") + viper.SetDefault("auth.clientID", "") + viper.SetDefault("auth.username", "") + viper.SetDefault("auth.password", "") } func init() { @@ -49,45 +39,32 @@ func init() { setDefaults() - err := viper.WriteConfigAs("GoCLI.backup.yaml") + err := viper.WriteConfigAs(".DurpCLI.yaml") if err != nil { fmt.Println(err) } - // Here you will define your flags and configuration settings. - // Cobra supports persistent flags, which, if defined here, - // will be global for your application. - - // Add my subcommand palette - rootCmd.AddCommand(info.InfoCmd) rootCmd.AddCommand(net.NetCmd) - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.toolbox.yaml)") + rootCmd.AddCommand(auth.AuthCmd) + rootCmd.AddCommand(cfg.Cfgcmd) + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.DurpCLI.yaml)") - // Cobra also supports local flags, which will only run - // when this action is called directly. rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } -// initConfig reads in config file and ENV variables if set. func initConfig() { if cfgFile != "" { - // Use config file from the flag. viper.SetConfigFile(cfgFile) } else { - // Find home directory. home, err := os.UserHomeDir() cobra.CheckErr(err) - // Search config in home directory with name ".toolbox" (without extension). viper.AddConfigPath(home) viper.SetConfigType("yaml") - viper.SetConfigName(".toolbox") + viper.SetConfigName(".DurpCLI") } - viper.AutomaticEnv() // read in environment variables that match + viper.AutomaticEnv() + viper.ReadInConfig() - // If a config file is found, read it in. - if err := viper.ReadInConfig(); err == nil { - fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) - } } diff --git a/go.mod b/go.mod index dbf2bbb..73dfd3a 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/DeveloperDurp/GoCLI +module gitlab.com/DeveloperDurp/DurpCLI go 1.17 diff --git a/img/.DS_Store b/img/.DS_Store deleted file mode 100644 index 0e73402..0000000 Binary files a/img/.DS_Store and /dev/null differ diff --git a/img/design.png b/img/design.png deleted file mode 100644 index 18d3736..0000000 Binary files a/img/design.png and /dev/null differ diff --git a/main.go b/main.go index c219ee6..39c9d4d 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,6 @@ -/* -Copyright © 2023 NAME HERE - -*/ package main -import "github.com/DeveloperDurp/GoCLI/cmd" +import "gitlab.com/DeveloperDurp/DurpCLI/cmd" func main() { cmd.Execute()