updates
This commit is contained in:
44
cmd/info/diskUsage.go
Normal file
44
cmd/info/diskUsage.go
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
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")
|
||||
}
|
||||
31
cmd/info/info.go
Normal file
31
cmd/info/info.go
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
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,5 +1,5 @@
|
||||
/*
|
||||
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
|
||||
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
|
||||
|
||||
*/
|
||||
package net
|
||||
@@ -11,15 +11,14 @@ import (
|
||||
// NetCmd represents the net command
|
||||
var NetCmd = &cobra.Command{
|
||||
Use: "net",
|
||||
Short: "Networking",
|
||||
Long: `Network based commands`,
|
||||
Short: "Net is a palette that contains network based commands",
|
||||
Long: ``,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
cmd.Help()
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
//rootCmd.AddCommand(netCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
|
||||
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
|
||||
|
||||
*/
|
||||
package net
|
||||
@@ -14,13 +14,14 @@ import (
|
||||
|
||||
var (
|
||||
urlPath string
|
||||
client = http.Client{
|
||||
Timeout: 2 * time.Second,
|
||||
// Logic
|
||||
client = http.Client{
|
||||
Timeout: time.Second * 2,
|
||||
}
|
||||
)
|
||||
|
||||
func ping(domain string) (int, error) {
|
||||
url := "https://" + domain
|
||||
url := "http://" + domain
|
||||
req, err := http.NewRequest("HEAD", url, nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -29,19 +30,15 @@ func ping(domain string) (int, error) {
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
resp.Body.Close()
|
||||
return resp.StatusCode, nil
|
||||
}
|
||||
|
||||
// pingCmd represents the ping command
|
||||
var pingCmd = &cobra.Command{
|
||||
Use: "ping",
|
||||
Short: "A brief description of your command",
|
||||
Long: `A longer description that spans multiple lines and likely contains examples
|
||||
and usage of using your command. 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.`,
|
||||
Short: "This pings a remote URL and returns the response",
|
||||
Long: ``,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
if resp, err := ping(urlPath); err != nil {
|
||||
@@ -53,6 +50,7 @@ to quickly create a Cobra application.`,
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
pingCmd.Flags().StringVarP(&urlPath, "url", "u", "", "The url to ping")
|
||||
|
||||
if err := pingCmd.MarkFlagRequired("url"); err != nil {
|
||||
|
||||
49
cmd/root.go
49
cmd/root.go
@@ -1,19 +1,24 @@
|
||||
/*
|
||||
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
|
||||
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"
|
||||
)
|
||||
|
||||
var cfgFile string
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "GoCLI",
|
||||
Use: "toolbox",
|
||||
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:
|
||||
@@ -35,19 +40,49 @@ func Execute() {
|
||||
}
|
||||
}
|
||||
|
||||
func addSubcommandPalettes() {
|
||||
rootCmd.AddCommand(net.NetCmd)
|
||||
func setDefaults() {
|
||||
viper.SetDefault("port", "8080")
|
||||
}
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
setDefaults()
|
||||
|
||||
fmt.Println("name:", viper.Get("name"))
|
||||
// Here you will define your flags and configuration settings.
|
||||
// Cobra supports persistent flags, which, if defined here,
|
||||
// will be global for your application.
|
||||
|
||||
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.GoCLI.yaml)")
|
||||
// Add my subcommand palette
|
||||
rootCmd.AddCommand(info.InfoCmd)
|
||||
rootCmd.AddCommand(net.NetCmd)
|
||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.toolbox.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")
|
||||
addSubcommandPalettes()
|
||||
}
|
||||
|
||||
// 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.AutomaticEnv() // read in environment variables that match
|
||||
|
||||
// If a config file is found, read it in.
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user