Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesread committed Aug 25, 2019
1 parent 11569c7 commit bbcea7a
Show file tree
Hide file tree
Showing 6 changed files with 418 additions and 674 deletions.
876 changes: 202 additions & 674 deletions LICENSE

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
default:
go build .
100 changes: 100 additions & 0 deletions cmd/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
Copyright © 2019 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"fmt"
prettytable "github.com/tatsushid/go-prettytable"
"github.com/spf13/cobra"
)

type TableRow = map[string]string;
type DataTable = map[int]TableRow;

func formatOutputJson(rows DataTable) string {
return "json";
}

func formatOutputTable(rows DataTable) string {
tbl, err := prettytable.NewTable([]prettytable.Column{
{Header: "COL1"},
{Header: "COL2", MinWidth: 6},
{Header: "COL3", AlignRight: true},
}...)
if err != nil {
panic(err)
}
tbl.Separator = " | "

for row := range rows {
for cel := range row {
tbl.AddRow(cel)
}
}

tbl.Print()

return "table";
}

func formatOutput(rows DataTable) string {
output := "format: ";

switch rootCmd.Flag("format").Value.String() {
case "table": return formatOutputTable(rows);
case "json": return formatOutputJson(rows);
}

return output;
}

func nodeList(cmd *cobra.Command, args []string) {
rows := make(DataTable, 0);

row := make(TableRow);
row["name"] = "one";
rows[0] = row;


fmt.Println(formatOutput(rows))
}

// nodeCmd represents the node command
var nodeCmd = &cobra.Command{
Use: "node",
Short: "Node details",
}

var nodeListCmd = &cobra.Command {
Use: "list",
Run: nodeList,
}

func init() {
nodeCmd.AddCommand(nodeListCmd)
rootCmd.AddCommand(nodeCmd)
rootCmd.PersistentFlags().StringP("format", "f", "table", "output format");

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

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

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// nodeCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
81 changes: 81 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Copyright © 2019 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"fmt"
"os"
"github.com/spf13/cobra"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"

)


var cfgFile string


// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "upsilon",
Short: "The upsilon command.",
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func init() {
cobra.OnInitialize(initConfig)

// 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/.upsilon-cli.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")
}


func initConfig() {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

viper.AddConfigPath(home)
viper.SetConfigName(".upsilon-cli")
}

viper.AutomaticEnv() // read in environment variables that match

if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}

11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/upsilonproject/upsilon-cli

go 1.12

require (
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.3.2
github.com/tatsushid/go-prettytable v0.0.0-20141013043238-ed2d14c29939
)
22 changes: 22 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Copyright © 2019 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main

import "github.com/upsilonproject/upsilon-cli/cmd"

func main() {
cmd.Execute()
}

0 comments on commit bbcea7a

Please sign in to comment.