Skip to content

Commit

Permalink
feat: complete v0.0.1 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
DeeStarks committed Oct 1, 2022
0 parents commit 4e7a00d
Show file tree
Hide file tree
Showing 11 changed files with 1,165 additions and 0 deletions.
78 changes: 78 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package config

import (
"os"
"path/filepath"
"runtime"
)

// Config is the configuration for the application
type Config struct {
// Name is the name of the application
Name string

// Version is the version of the application
Version string

// Author is the author of the application
Author string

// Email is the email of the author
Email string

// Description is the description of the application
Description string

// License is the license of the application
License string

// Path is the path to the application
Path string

// ConfigPath is the path to the configuration file
ConfigPath string

// ConfigName is the name of the configuration file
ConfigName string

// ConfigType is the type of the configuration file
ConfigType string

// LogPath is the path to the log file
LogPath string
}

// New creates a new configuration
func New() *Config {
return &Config{
Name: "routnd",
Version: "0.0.1",
Author: "DeeStarks",
Email: "danielonoja246@gmail.com",
Description: "A CLI tool for executing commands/processes in the background",
License: "MIT",
Path: getPath(),
ConfigPath: getConfigPath(),
ConfigName: "config",
ConfigType: "yaml",
LogPath: getLogPath(),
}
}

// getPath returns the path to the application
func getPath() string {
_, filename, _, _ := runtime.Caller(0)
return filepath.Dir(filename)
}

// getConfigPath returns the path to the configuration file
func getConfigPath() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, ".routnd", "config")
}

// getLogPath returns the path to the log file
func getLogPath() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, ".routnd", "logs")
}
30 changes: 30 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module github.com/deestarks/routnd

go 1.18

require (
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.5.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.13.0
)

require (
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
489 changes: 489 additions & 0 deletions go.sum

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions internal/commands/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package commands

import (
"os"
"strconv"

"github.com/deestarks/routnd/config"
"github.com/deestarks/routnd/internal/process"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

// List
var ListCmd = &cobra.Command{
Use: "list",
Short: "List all the processes",
Long: `List all the processes that are currently running in the background`,
Run: func(cmd *cobra.Command, args []string) {
list()
},
}

func list() {
// Get the config
cfg := config.New()

// Get the processes
processes := process.GetProcesses(cfg)

var data [][]string

for _, p := range processes {
data = append(data, []string{strconv.Itoa(p.PID), p.Name, p.Command, p.CreationTime})
}

// Create the table
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"PID", "Name", "Command", "Created"})
// table.SetBorder(false)
table.AppendBulk(data)
table.Render()
}

// Status
var StatusCmd = &cobra.Command{
Use: "status",
Short: "Get the status of a process",
Long: `Get the status of a process that is currently running in the background`,
Run: func(cmd *cobra.Command, args []string) {
status(cmd.Flags())
},
}

// Flags
func init() {
StatusCmd.Flags().StringP("name", "n", "", "The name of the process")
}

func status(flags *pflag.FlagSet) {
if name, err := flags.GetString("name"); err != nil {
panic(err)
} else {
process.Status(config.New(), name)
}
}
50 changes: 50 additions & 0 deletions internal/commands/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package commands

import (
"github.com/deestarks/routnd/config"
"github.com/deestarks/routnd/internal/process"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

var LogsCmd = &cobra.Command{
Use: "logs",
Short: "Get the logs of a process",
Long: `Get the logs of a process that is currently running in the background`,
Run: func(cmd *cobra.Command, args []string) {
logs(cmd.Flags())
},
}

// Flags
func init() {
LogsCmd.Flags().StringP("name", "n", "", "The name of the process")
LogsCmd.Flags().BoolP("follow", "f", false, "Follow the logs")
LogsCmd.Flags().IntP("tail", "t", 0, "Number of the last lines to show")
}

func logs(flags *pflag.FlagSet) {
var (
name string
follow bool
tail int
err error
)

// Get the flags
if name, err = flags.GetString("name"); err != nil {
panic(err)
}
if follow, err = flags.GetBool("follow"); err != nil {
panic(err)
}
if tail, err = flags.GetInt("tail"); err != nil {
panic(err)
}

// Get the config
cfg := config.New()

// View the logs
process.Logs(cfg, name, tail, follow)
}
50 changes: 50 additions & 0 deletions internal/commands/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package commands

import (
"github.com/deestarks/routnd/config"
"github.com/deestarks/routnd/internal/process"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

var StartCmd = &cobra.Command{
Use: "start",
Short: "Start a process",
Long: `Start a process in the background`,
Run: func(cmd *cobra.Command, args []string) {
start(cmd.Flags())
},
}

// Add the flags
func init() {
StartCmd.Flags().StringP("name", "n", "", "The name of the process")
StartCmd.Flags().StringP("command", "c", "", "The command to run")
StartCmd.Flags().StringP("log-file", "l", "", "The log file to save the output to (optional)")
}

func start(flags *pflag.FlagSet) {
// Get the name
name, err := flags.GetString("name")
if err != nil {
panic(err)
}

// Get the command
command, err := flags.GetString("command")
if err != nil {
panic(err)
}

// Get log file
logFile, err := flags.GetString("log-file")
if err != nil {
panic(err)
}

// Get the config
cfg := config.New()

// Start the process
process.Start(cfg, name, command, logFile)
}
30 changes: 30 additions & 0 deletions internal/commands/stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package commands

import (
"github.com/deestarks/routnd/config"
"github.com/deestarks/routnd/internal/process"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

var StopCmd = &cobra.Command{
Use: "stop",
Short: "Stop a process",
Long: `Stop a process that is currently running in the background`,
Run: func(cmd *cobra.Command, args []string) {
stop(cmd.Flags())
},
}

// Flags
func init() {
StopCmd.Flags().StringP("name", "n", "", "The name of the process")
}

func stop(flags *pflag.FlagSet) {
if name, err := flags.GetString("name"); err != nil {
panic(err)
} else {
process.Stop(config.New(), name)
}
}
1 change: 1 addition & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package logger
Loading

0 comments on commit 4e7a00d

Please sign in to comment.