Add initial structs for CLI commands

Signed-off-by: fbugarski <filipbugarski@gmail.com>
This commit is contained in:
fbugarski
2023-06-26 11:53:02 +02:00
parent 5a03d2ae4a
commit 93b47a5897
8 changed files with 90 additions and 183 deletions
+20
View File
@@ -0,0 +1,20 @@
package cli
import (
"fmt"
"github.com/spf13/cobra"
)
func NewAlgorithmsCmd() *cobra.Command {
return &cobra.Command{
Use: "upload-algorithm",
Short: "Upload an algorithm binary",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
algorithmFile := args[0]
fmt.Println("Uploading algorithm binary:", algorithmFile)
},
}
}
-73
View File
@@ -1,73 +0,0 @@
/*
Copyright © 2021 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"
"io"
"net/http"
"os"
"github.com/spf13/cobra"
)
// getCmd represents the get command
var getCmd = &cobra.Command{
Use: "get",
Short: "This command will get the desired Gopher",
Long: `This get command will call GitHub respository in order to return the desired Gopher.`,
Run: func(cmd *cobra.Command, args []string) {
var gopherName = "dr-who"
if len(args) >= 1 && args[0] != "" {
gopherName = args[0]
}
URL := "https://github.com/scraly/gophers/raw/main/" + gopherName + ".png"
fmt.Println("Try to get '" + gopherName + "' Gopher...")
// Get the data
response, err := http.Get(URL)
if err != nil {
fmt.Println(err)
}
defer response.Body.Close()
if response.StatusCode == 200 {
// Create the file
out, err := os.Create(gopherName + ".png")
if err != nil {
fmt.Println(err)
}
defer out.Close()
// Writer the body to file
_, err = io.Copy(out, response.Body)
if err != nil {
fmt.Println(err)
}
fmt.Println("Perfect! Just saved in " + out.Name() + "!")
} else {
fmt.Println("Error: " + gopherName + " not exists! :-(")
}
},
}
func init() {
rootCmd.AddCommand(getCmd)
}
-81
View File
@@ -1,81 +0,0 @@
/*
Copyright © 2021 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"
"github.com/spf13/viper"
)
var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "go-gopher-cli",
Short: "Gopher CLI in Go",
Long: `Gopher CLI application written in Go.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
// 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() {
cobra.CheckErr(rootCmd.Execute())
}
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/.go-gopher-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")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := os.UserHomeDir()
cobra.CheckErr(err)
// Search config in home directory with name ".go-gopher-cli" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".go-gopher-cli")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
}
+20
View File
@@ -0,0 +1,20 @@
package cli
import (
"fmt"
"github.com/spf13/cobra"
)
func NewDatasetsCmd() *cobra.Command {
return &cobra.Command{
Use: "upload-dataset",
Short: "Upload a dataset CSV file",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
datasetFile := args[0]
fmt.Println("Uploading dataset CSV:", datasetFile)
},
}
}
-22
View File
@@ -1,22 +0,0 @@
/*
Copyright © 2021 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/ultravioletrs/agent/cli/cmd"
func main() {
cmd.Execute()
}
+18
View File
@@ -0,0 +1,18 @@
package cli
import (
"fmt"
"github.com/spf13/cobra"
)
func NewResultsCmd() *cobra.Command {
return &cobra.Command{
Use: "retrieve-result",
Short: "Retrieve computation result file",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Retrieving computation result file")
},
}
}
-7
View File
@@ -1,10 +1,3 @@
//
// Copyright (c) 2019
// Mainflux
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
+32
View File
@@ -0,0 +1,32 @@
package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/ultravioletrs/agent/cli"
)
var rootCmd = &cobra.Command{
Use: "cli-app",
Short: "CLI application for Computation Service API",
Run: func(cmd *cobra.Command, args []string) {
// Display help if no subcommand is provided
cmd.Help()
},
}
func init() {
rootCmd.AddCommand(cli.NewAlgorithmsCmd())
rootCmd.AddCommand(cli.NewDatasetsCmd())
rootCmd.AddCommand(cli.NewResultsCmd())
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}