add cobra to implement cli interface for different commands

This commit is contained in:
Jakob Schrettenbrunner 2017-06-29 12:21:59 +02:00
parent f183654996
commit 50e27db1c4
4 changed files with 60 additions and 24 deletions

48
command/root.go Normal file
View File

@ -0,0 +1,48 @@
package command
import (
"fmt"
"github.com/schrej/wings/api"
"github.com/schrej/wings/config"
"github.com/schrej/wings/tools"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
const (
// Version of pterodactyld
Version = "0.0.1-alpha"
)
var RootCommand = &cobra.Command{
Use: "wings",
Short: "",
Long: "",
Run: run,
}
func init() {
}
func Execute() {
RootCommand.Execute()
}
func run(cmd *cobra.Command, args []string) {
fmt.Println("Loading configuration")
if err := config.LoadConfiguration(nil); err != nil {
log.WithError(err).Fatal("Failed to find configuration file")
}
tools.ConfigureLogging()
log.Info("Starting wings.go version ", Version)
// Load configuration
log.Info("Loading configuration...")
log.Info("Starting api webserver")
api := api.NewAPI()
api.Listen()
}

8
glide.lock generated
View File

@ -1,5 +1,5 @@
hash: 4954000649d7a74d198904957adae4d4a88f6710c254f87930fbf6457b07eddb hash: df8c67e65e52dabe58d7e178fd208c04518f7f30cf6de7c4ee11d4e4440a55a5
updated: 2017-06-27T12:42:33.899081413+02:00 updated: 2017-06-29T12:20:46.267916965+02:00
imports: imports:
- name: bitbucket.org/tebeka/strftime - name: bitbucket.org/tebeka/strftime
version: 2194253a23c090a4d5953b152a3c63fb5da4f5a4 version: 2194253a23c090a4d5953b152a3c63fb5da4f5a4
@ -25,6 +25,8 @@ imports:
- json/parser - json/parser
- json/scanner - json/scanner
- json/token - json/token
- name: github.com/inconshreveable/mousetrap
version: 76626ae9c91c4f2a10f34cad8ce83ea42c93bb75
- name: github.com/lestrrat/go-file-rotatelogs - name: github.com/lestrrat/go-file-rotatelogs
version: ab335c655133cea61d8164853c1ed0e97d6c77cb version: ab335c655133cea61d8164853c1ed0e97d6c77cb
- name: github.com/magiconair/properties - name: github.com/magiconair/properties
@ -49,6 +51,8 @@ imports:
- mem - mem
- name: github.com/spf13/cast - name: github.com/spf13/cast
version: acbeb36b902d72a7a4c18e8f3241075e7ab763e4 version: acbeb36b902d72a7a4c18e8f3241075e7ab763e4
- name: github.com/spf13/cobra
version: 4d647c8944eb42504a714e57e97f244ed6344722
- name: github.com/spf13/jwalterweatherman - name: github.com/spf13/jwalterweatherman
version: 0efa5202c04663c757d84f90f5219c1250baf94f version: 0efa5202c04663c757d84f90f5219c1250baf94f
- name: github.com/spf13/pflag - name: github.com/spf13/pflag

View File

@ -9,6 +9,7 @@ import:
- package: github.com/sirupsen/logrus - package: github.com/sirupsen/logrus
version: ~1.0.0 version: ~1.0.0
- package: github.com/spf13/viper - package: github.com/spf13/viper
- package: github.com/spf13/cobra
testImport: testImport:
- package: github.com/stretchr/testify - package: github.com/stretchr/testify
version: ~1.1.4 version: ~1.1.4

27
main.go
View File

@ -2,31 +2,14 @@ package main
import ( import (
"fmt" "fmt"
"os"
"github.com/schrej/wings/api" "github.com/schrej/wings/command"
"github.com/schrej/wings/config"
"github.com/schrej/wings/tools"
log "github.com/sirupsen/logrus"
)
const (
// Version of pterodactyld
Version = "0.0.1-alpha"
) )
func main() { func main() {
fmt.Println("Loading configuration") if err := command.RootCommand.Execute(); err != nil {
if err := config.LoadConfiguration(nil); err != nil { fmt.Println(err)
log.WithError(err).Fatal("Failed to find configuration file") os.Exit(1)
} }
tools.ConfigureLogging()
log.Info("Starting wings.go version ", Version)
// Load configuration
log.Info("Loading configuration...")
log.Info("Starting api webserver")
api := api.NewAPI()
api.Listen()
} }