update durpcli
This commit is contained in:
6
.DurpCLI.yaml
Normal file
6
.DurpCLI.yaml
Normal file
@@ -0,0 +1,6 @@
|
||||
auth:
|
||||
clientid: ""
|
||||
granttype: client_credentials
|
||||
password: ""
|
||||
url: https://authentik.durp.info/application/o/token/
|
||||
username: ""
|
||||
@@ -1 +0,0 @@
|
||||
port: "8080"
|
||||
@@ -1,4 +0,0 @@
|
||||
cmd:
|
||||
info:
|
||||
diskUsage:
|
||||
defaultDirectory: "/"
|
||||
18
cmd/auth/auth.go
Normal file
18
cmd/auth/auth.go
Normal 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
104
cmd/auth/generateToken.go
Normal 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
18
cmd/cfg/cfg.go
Normal 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
26
cmd/cfg/newconfig.go
Normal 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)
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
@@ -1,14 +1,9 @@
|
||||
/*
|
||||
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
|
||||
|
||||
*/
|
||||
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")
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
/*
|
||||
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
|
||||
|
||||
*/
|
||||
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")
|
||||
}
|
||||
|
||||
59
cmd/root.go
59
cmd/root.go
@@ -1,38 +1,24 @@
|
||||
/*
|
||||
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
|
||||
|
||||
*/
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
2
go.mod
2
go.mod
@@ -1,4 +1,4 @@
|
||||
module github.com/DeveloperDurp/GoCLI
|
||||
module gitlab.com/DeveloperDurp/DurpCLI
|
||||
|
||||
go 1.17
|
||||
|
||||
|
||||
BIN
img/.DS_Store
vendored
BIN
img/.DS_Store
vendored
Binary file not shown.
BIN
img/design.png
BIN
img/design.png
Binary file not shown.
|
Before Width: | Height: | Size: 148 KiB |
Reference in New Issue
Block a user