update durpcli

This commit is contained in:
2023-05-27 11:43:24 -05:00
parent f13224733f
commit ca21cb9811
18 changed files with 193 additions and 157 deletions

6
.DurpCLI.yaml Normal file
View File

@@ -0,0 +1,6 @@
auth:
clientid: ""
granttype: client_credentials
password: ""
url: https://authentik.durp.info/application/o/token/
username: ""

View File

@@ -1 +0,0 @@
port: "8080"

View File

@@ -1,4 +0,0 @@
cmd:
info:
diskUsage:
defaultDirectory: "/"

View File

View File

@@ -1 +0,0 @@
![toolboxdesign](./img/design.png)

18
cmd/auth/auth.go Normal file
View File

@@ -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() {
}

104
cmd/auth/generateToken.go Normal file
View File

@@ -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)
}

18
cmd/cfg/cfg.go Normal file
View File

@@ -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() {
}

26
cmd/cfg/newconfig.go Normal file
View File

@@ -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)
}

View File

@@ -1,44 +0,0 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
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")
}

View File

@@ -1,31 +0,0 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
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")
}

View File

@@ -1,14 +1,9 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package net package net
import ( import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// NetCmd represents the net command
var NetCmd = &cobra.Command{ var NetCmd = &cobra.Command{
Use: "net", Use: "net",
Short: "Net is a palette that contains network based commands", Short: "Net is a palette that contains network based commands",
@@ -20,13 +15,4 @@ var NetCmd = &cobra.Command{
func init() { 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")
} }

View File

@@ -1,7 +1,3 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package net package net
import ( import (
@@ -14,8 +10,7 @@ import (
var ( var (
urlPath string urlPath string
// Logic client = http.Client{
client = http.Client{
Timeout: time.Second * 2, Timeout: time.Second * 2,
} }
) )
@@ -34,7 +29,6 @@ func ping(domain string) (int, error) {
return resp.StatusCode, nil return resp.StatusCode, nil
} }
// pingCmd represents the ping command
var pingCmd = &cobra.Command{ var pingCmd = &cobra.Command{
Use: "ping", Use: "ping",
Short: "This pings a remote URL and returns the response", Short: "This pings a remote URL and returns the response",
@@ -57,13 +51,5 @@ func init() {
fmt.Println(err) fmt.Println(err)
} }
// Here you will define your flags and configuration settings.
NetCmd.AddCommand(pingCmd) 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")
} }

View File

@@ -1,38 +1,24 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd package cmd
import ( import (
"fmt" "fmt"
"os" "os"
"github.com/DeveloperDurp/GoCLI/cmd/info"
"github.com/DeveloperDurp/GoCLI/cmd/net"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "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 var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "GoCLI", Use: "DurpCLI",
Short: "A brief description of your application", Short: "CLI Tool made for Durp",
Long: `A longer description that spans multiple lines and likely contains Long: ``,
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) { },
} }
// 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() { func Execute() {
err := rootCmd.Execute() err := rootCmd.Execute()
if err != nil { if err != nil {
@@ -41,7 +27,11 @@ func Execute() {
} }
func setDefaults() { 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() { func init() {
@@ -49,45 +39,32 @@ func init() {
setDefaults() setDefaults()
err := viper.WriteConfigAs("GoCLI.backup.yaml") err := viper.WriteConfigAs(".DurpCLI.yaml")
if err != nil { if err != nil {
fmt.Println(err) 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.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") rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
} }
// initConfig reads in config file and ENV variables if set.
func initConfig() { func initConfig() {
if cfgFile != "" { if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile) viper.SetConfigFile(cfgFile)
} else { } else {
// Find home directory.
home, err := os.UserHomeDir() home, err := os.UserHomeDir()
cobra.CheckErr(err) cobra.CheckErr(err)
// Search config in home directory with name ".toolbox" (without extension).
viper.AddConfigPath(home) viper.AddConfigPath(home)
viper.SetConfigType("yaml") 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())
}
} }

2
go.mod
View File

@@ -1,4 +1,4 @@
module github.com/DeveloperDurp/GoCLI module gitlab.com/DeveloperDurp/DurpCLI
go 1.17 go 1.17

BIN
img/.DS_Store vendored

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 148 KiB

View File

@@ -1,10 +1,6 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package main package main
import "github.com/DeveloperDurp/GoCLI/cmd" import "gitlab.com/DeveloperDurp/DurpCLI/cmd"
func main() { func main() {
cmd.Execute() cmd.Execute()