Skip to content

Commit

Permalink
feat: Added cpu command
Browse files Browse the repository at this point in the history
  • Loading branch information
kerimkaan committed May 24, 2024
1 parent 1281a33 commit 4801b83
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 9 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
Genius is a simple and easy to use CLI tool for gathering information about a
system. It is valid for Linux and MacOS systems.

## Download

You can download the latest version of Genius from the [releases page](https://github.com/kerimkaan/genius/releases)

## Installation

```bash
git clone https://github.com/kerimkaan/genius.git
cd genius
go mod download
go build -ldflags="-s -w" -o /usr/local/bin/genius main.go
CGO_ENABLED="0" go build -ldflags="-s -w" -o /usr/local/bin/genius main.go
```

## Usage
Expand Down
71 changes: 71 additions & 0 deletions cmd/cpu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright © 2024 Kerim Kaan Dönmez <[email protected]>
*/
package cmd

import (
"fmt"

"github.com/shirou/gopsutil/v3/cpu"
"github.com/spf13/cobra"
)

// cpuCmd represents the cpu command
var cpuCmd = &cobra.Command{
Use: "cpu",
Aliases: []string{"c"},
Short: "Detailed CPU information",
Long: `Get detailed CPU information such as physical cores, logical cores, CPU model, CPU cores, CPU usage percentage and CPU times.`,
Run: func(cmd *cobra.Command, args []string) {
physicalCores, _ := cpu.Counts(false)

logicalCores, _ := cpu.Counts(true)

cpuInfo, err := cpu.Info()
if err != nil {
fmt.Println(err)
}

percents, err := cpu.Percent(0, true)
if err != nil {
if err.Error() == "not implemented yet" {
percents = nil
}
}

times, err := cpu.Times(true)
if err != nil {
if err.Error() == "not implemented yet" {
times = []cpu.TimesStat{}
}
}

fmt.Println("Physical cores:", physicalCores)
fmt.Println("Logical cores:", logicalCores)
fmt.Println("CPU Info:", cpuInfo)
if percents == nil {
fmt.Println("CPU Percent: Not implemented yet")
} else {
fmt.Println("CPU Percent:", percents)
}
if len(times) == 0 {
fmt.Println("CPU Times: Not implemented yet")
} else {
fmt.Println("CPU Times:", times)
}
},
}

func init() {
rootCmd.AddCommand(cpuCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// cpuCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// cpuCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
7 changes: 5 additions & 2 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import (

// infoCmd represents the info command
var infoCmd = &cobra.Command{
Use: "info",
Short: "Get a brief system information",
Use: "info",
Aliases: []string{"i"},
Short: "Get a brief system information",
Long: `
Get a brief system information such as hostname, OS, architecture, kernel version, platform, platform family,
platform version, virtualization system, virtualization role, hostID, uptime, boot time, procs, load average, CPU model,
Expand Down Expand Up @@ -65,11 +66,13 @@ disk filesystem, interface name, interface hardware address, interface MTU, IPv4
log.Println(err)
panic(err)
}
// Swap memory information
swapInfo, err := mem.SwapMemory()
if err != nil {
log.Println(err)
panic(err)
}

// Get disk usage information
diskInfo, err := disk.Usage("/")
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "genius",
Short: "Genius is a CLI tool to get a brief system information.",
Long: `Genius is a CLI tool to get a brief system information such as platform, host, CPU, memory, disk, and network.`,
Use: "genius",
Version: constants.VERSION,
Short: "Genius is a CLI tool to get a brief system information.",
Long: `Genius is a CLI tool to get a brief system information such as platform, host, CPU, memory, disk, and network.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Expand All @@ -33,7 +34,6 @@ func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.Version = constants.VERSION
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.genius.yaml)")

// Cobra also supports local flags, which will only run
Expand Down
4 changes: 2 additions & 2 deletions constants/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package constants

const (
// Version is the current version of the application
VERSION = "0.0.2"
VERSION = "0.0.3"
MAJOR = 0
MINOR = 0
PATCH = 2
PATCH = 3
)

0 comments on commit 4801b83

Please sign in to comment.