Skip to content

Commit 2521a1f

Browse files
committed
[+] root functions [+]
1 parent e8e2ab6 commit 2521a1f

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

cmd/root.go

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
homedir "github.com/mitchellh/go-homedir"
9+
"github.com/spf13/cobra"
10+
"github.com/spf13/viper"
11+
12+
overallGraph "github.com/krishpranav/gotop/src/display/general"
13+
"github.com/krishpranav/gotop/src/general"
14+
info "github.com/krishpranav/gotop/src/general"
15+
"github.com/krishpranav/gotop/src/utils"
16+
"golang.org/x/sync/errgroup"
17+
)
18+
19+
const (
20+
defaultOverallRefreshRate = 1000
21+
defaultConfigFileLocation = ""
22+
defaultCPUBehavior = false
23+
)
24+
25+
var cfgFile string
26+
27+
// rootCmd represents the base command when called without any subcommands
28+
var rootCmd = &cobra.Command{
29+
Use: "gotop",
30+
Short: "gotop is a system and resource monitor written in golang",
31+
Long: `gotop is a system and resource monitor written in golang.
32+
While using a TUI based command, press ? to get information about key bindings (if any) for that command.`,
33+
RunE: func(cmd *cobra.Command, args []string) error {
34+
overallRefreshRate, _ := cmd.Flags().GetUint64("refresh")
35+
36+
if overallRefreshRate < 1000 {
37+
return fmt.Errorf("invalid refresh rate: minimum refresh rate is 1000(ms)")
38+
}
39+
40+
cpuLoadFlag, _ := cmd.Flags().GetBool("cpuinfo")
41+
if cpuLoadFlag {
42+
cpuLoad := info.NewCPULoad()
43+
dataChannel := make(chan *info.CPULoad, 1)
44+
45+
eg, ctx := errgroup.WithContext(context.Background())
46+
47+
eg.Go(func() error {
48+
return info.GetCPULoad(ctx, cpuLoad, dataChannel, uint64(4*overallRefreshRate/5))
49+
})
50+
51+
eg.Go(func() error {
52+
return overallGraph.RenderCPUinfo(ctx, dataChannel, overallRefreshRate)
53+
})
54+
55+
if err := eg.Wait(); err != nil {
56+
if err != general.ErrCanceledByUser {
57+
fmt.Printf("Error: %v\n", err)
58+
}
59+
}
60+
61+
} else {
62+
dataChannel := make(chan utils.DataStats, 1)
63+
64+
eg, ctx := errgroup.WithContext(context.Background())
65+
66+
eg.Go(func() error {
67+
return general.GlobalStats(ctx, dataChannel, uint64(4*overallRefreshRate/5))
68+
})
69+
eg.Go(func() error {
70+
return overallGraph.RenderCharts(ctx, dataChannel, overallRefreshRate)
71+
})
72+
73+
if err := eg.Wait(); err != nil {
74+
if err != general.ErrCanceledByUser {
75+
fmt.Printf("Error: %v\n", err)
76+
}
77+
}
78+
}
79+
80+
return nil
81+
},
82+
}
83+
84+
func Execute() {
85+
if err := rootCmd.Execute(); err != nil {
86+
os.Exit(1)
87+
}
88+
}
89+
90+
func init() {
91+
cobra.OnInitialize(initConfig)
92+
rootCmd.PersistentFlags().StringVar(
93+
&cfgFile,
94+
"config",
95+
defaultConfigFileLocation,
96+
"config file (default is $HOME/.gotop.yaml)",
97+
)
98+
99+
rootCmd.Flags().Uint64P(
100+
"refresh",
101+
"r",
102+
defaultOverallRefreshRate,
103+
"Overall stats UI refreshes rate in milliseconds greater than 1000",
104+
)
105+
106+
rootCmd.Flags().BoolP(
107+
"cpuinfo",
108+
"c",
109+
defaultCPUBehavior,
110+
"Info about the CPU Load over all CPUs",
111+
)
112+
}
113+
114+
// initConfig reads in config file and ENV variables if set.
115+
func initConfig() {
116+
if cfgFile != "" {
117+
// Use config file from the flag.
118+
viper.SetConfigFile(cfgFile)
119+
} else {
120+
// Find home directory.
121+
home, err := homedir.Dir()
122+
if err != nil {
123+
fmt.Println(err)
124+
os.Exit(1)
125+
}
126+
127+
viper.AddConfigPath(home)
128+
viper.SetConfigName(".gotop")
129+
}
130+
131+
viper.AutomaticEnv()
132+
133+
if err := viper.ReadInConfig(); err == nil {
134+
fmt.Println("Using config file:", viper.ConfigFileUsed())
135+
}
136+
}

0 commit comments

Comments
 (0)