Skip to content

Commit

Permalink
Drivers: working on basic implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
deven96 committed Oct 18, 2021
1 parent 93ee34a commit e582305
Show file tree
Hide file tree
Showing 13 changed files with 865 additions and 129 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
name: Test
jobs:
test:
strategy:
matrix:
go-version: [1.16.x, 1.17.x]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Test
# run docker ssh container for ssh tests
run: go test -v ./...
140 changes: 11 additions & 129 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,129 +1,11 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
# Test binary, built with `go test -c`
*.test
#
# # Output of the go coverage tool, specifically when used with LiteIDE
*.out
#
# # Dependency directories (remove the comment below to include it)
vendor/
*.swp
*.swo
*.swp
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# saido
saido means monitor in Hausa


[![Build Status](https://github.com/bisoncorps/saido/workflows/Test/badge.svg)](https://github.com/bisoncorps/saido/actions)
46 changes: 46 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
)

var (
// Verbose : Should display verbose logs
verbose bool
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "saido",
Short: "TUI for monitoring specific host metrics",
Long: `Saido`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Log only errors except in Verbose mode
if verbose {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
fmt.Println("\n\nSaido - Bisoncorp (2020) (https://github.com/bisoncorps/saido)")
},
}

// 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() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func init() {
// cobra.OnInitialize(initConfig)
viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))
}
File renamed without changes.
9 changes: 9 additions & 0 deletions driver/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package driver

// Driver : specification of functions to be defined by every Driver
type Driver interface {
readFile(path string) (string, error)
runCommand(command string) (string, error)
// shows the driver details, not sure if we should be showing OS name
getDetails() string
}
36 changes: 36 additions & 0 deletions driver/local.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package driver

import (
"fmt"
log "github.com/sirupsen/logrus"
"io/ioutil"
"os/exec"
"runtime"
"strings"
)

// Local : Driver for handling local executions
type Local struct{}

func (d *Local) readFile(path string) (string, error) {
log.Debugf("Reading content from %s", path)
content, err := ioutil.ReadFile(path)
if err != nil {
return ``, err
}
return string(content), nil
}

func (d *Local) runCommand(command string) (string, error) {
cmdArgs := strings.Fields(command)
log.Debugf("Running command `%s` ", command)
out, err := exec.Command(cmdArgs[0], cmdArgs[1:]...).Output()
if err != nil {
return ``, err
}
return string(out), nil
}

func (d *Local) getDetails() string {
return fmt.Sprintf(`Local - %s`, runtime.GOOS)
}
14 changes: 14 additions & 0 deletions driver/local_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package driver

import (
"strings"
"testing"
)

func TestUnixLocalRunCommand(t *testing.T) {
d := Local{}
output, err := d.runCommand(`ps -A`)
if err != nil || !strings.Contains(output, "PID") {
t.Error(err)
}
}
22 changes: 22 additions & 0 deletions driver/local_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package driver

import (
"strings"
"testing"
)

func TestWindowsRunCommand(t *testing.T) {
d := Local{}
output, err := d.runCommand(`tasklist`)
if err != nil || !strings.Contains(output, "PID") {
t.Error(err)
}
}

func TestWindowsLocalGetDetails(t *testing.T) {
d := Local{}
output := d.getDetails()
if output != "Local - windows" {
t.Error(output)
}
}
1 change: 1 addition & 0 deletions driver/sshnix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package driver
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/bisoncorps/saido

go 1.14

require (
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.9.0
)
Loading

0 comments on commit e582305

Please sign in to comment.