Skip to content

Commit

Permalink
add api server
Browse files Browse the repository at this point in the history
  • Loading branch information
redradrat committed Oct 18, 2020
1 parent 080cbf1 commit a0b4bce
Show file tree
Hide file tree
Showing 449 changed files with 117,926 additions and 7,466 deletions.
85 changes: 0 additions & 85 deletions api/server.gen.go

This file was deleted.

21 changes: 0 additions & 21 deletions api/server.go

This file was deleted.

52 changes: 0 additions & 52 deletions api/types.gen.go

This file was deleted.

9 changes: 7 additions & 2 deletions cmd/addRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

var repoUser string
var repoPass string
var repoRef string

// addRepoCmd represents the add command
var addRepoCmd = &cobra.Command{
Expand Down Expand Up @@ -73,13 +74,16 @@ var addRepoCmd = &cobra.Command{
}
}

mod := repositories.AddRepository(repositories.Repository{
mod, err := repositories.AddRepository(repositories.Repository{
Name: name,
GitRepository: repositories.GitRepository{
URL: repoUrl,
GitRef: "refs/heads/master",
GitRef: repoRef,
},
})
if err != nil {
PrintError("unable to add repository: %v", err)
}
mods = append(mods, mod)
err = repositories.UpdateRegistry(mods...)
if err != nil {
Expand All @@ -102,4 +106,5 @@ func init() {
// is called directly, e.g.:
addRepoCmd.Flags().StringVarP(&repoUser, "username", "u", "", "The username for this repository.")
addRepoCmd.Flags().StringVarP(&repoPass, "password", "p", "", "The password for this repository.")
addRepoCmd.Flags().StringVarP(&repoRef, "ref", "r", "", "The gitref to use for this repository.")
}
59 changes: 58 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ package cmd
import (
"fmt"
"os"
"path/filepath"

"github.com/redradrat/kable/pkg/repositories"

"github.com/spf13/viper"

"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{}
var cfgFile 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.
Expand All @@ -24,4 +30,55 @@ func Execute() {
}
}

func init() {}
func init() {
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.kable/userconfig.json)")
cobra.OnInitialize(initConfig)
bindFlags(rootCmd, viper.GetViper())
}

func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Search config in home directory with name ".cobra" (without extension).
viper.AddConfigPath(repositories.KableDir)
viper.SetConfigName("settings")
viper.SetConfigType("json")
viper.Set(repositories.StoreKey, repositories.LocalStoreConfigMap().Map())

fullpath := filepath.Join(repositories.KableDir, "settings.json")
if _, err := os.Stat(fullpath); err != nil {
if os.IsNotExist(err) {
err = viper.WriteConfigAs(fullpath)
if err != nil {
fmt.Println("Error writing config file:", err)
os.Exit(1)
}
}
}
}

viper.SetEnvPrefix("KABLE")

viper.AutomaticEnv()

}

// Bind each cobra flag to its associated viper configuration (config file and environment variable)
func bindFlags(cmd *cobra.Command, v *viper.Viper) {
//cmd.Flags().VisitAll(func(f *pflag.Flag) {
// // Environment variables can't have dashes in them, so bind them to their equivalent
// // keys with underscores, e.g. --favorite-color to STING_FAVORITE_COLOR
// if strings.Contains(f.Name, "-") {
// envVarSuffix := strings.ToUpper(strings.ReplaceAll(f.Name, "-", "_"))
// v.BindEnv(f.Name, fmt.Sprintf("%s_%s", envPrefix, envVarSuffix))
// }
//
// // Apply the viper config value to the flag when the flag is not set and viper has a value
// if !f.Changed && v.IsSet(f.Name) {
// val := v.Get(f.Name)
// cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val))
// }
//})
}
41 changes: 25 additions & 16 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,44 @@ limitations under the License.
package cmd

import (
"github.com/labstack/echo/v4"
"github.com/redradrat/kable/api"
"fmt"
"strings"

"github.com/redradrat/kable/pkg/repositories"

"github.com/spf13/viper"

"github.com/redradrat/kable/pkg/api"
"github.com/spf13/cobra"
)

const serverAddressKey = "address"
const serverPortKey = "port"
const etcdEndpoints = "etcdEndpoints"
const etcdTimeout = "etcdTimeout"

// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Run kable as a server",
Long: `Runs kable as a server expecting payloads via a REST interface.`,
Example: `kable serve --server-address 127.0.0.1 --server-port 2020
KABLE_SERVERADDRESS=127.0.0.1 KABLE_SERVERPORT kable serve`,
Run: func(cmd *cobra.Command, args []string) {
serv := api.Serv{}
e := echo.New()
api.RegisterHandlers(e, &serv)
e.Static("/", "kable.v1.yaml")
e.Logger.Fatal(e.Start("localhost:1323"))
viper.Set(repositories.StoreKey, repositories.EtcdStoreConfigMap(strings.Split(viper.GetString(etcdEndpoints), ","), viper.GetDuration(etcdTimeout)).Map())
api.StartUp(fmt.Sprintf("%s:%s", viper.Get(serverAddressKey), viper.Get(serverPortKey)))
},
}

func init() {
rootCmd.AddCommand(serveCmd)

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

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

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// serveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
serveCmd.Flags().StringP("server-address", "a", "0.0.0.0", "The adress to bind the server to")
serveCmd.Flags().StringP("server-port", "p", "1323", "The port for the kable api to listen on")
serveCmd.Flags().String("etcd-endpoints", "", "The etcd endpoints to use")
serveCmd.Flags().String("etcd-timeout", "5000", "The timeout for etcd interactions in milliseconds")
viper.BindPFlag(serverAddressKey, serveCmd.Flags().Lookup("server-address"))
viper.BindPFlag(serverPortKey, serveCmd.Flags().Lookup("server-port"))
viper.BindPFlag(etcdEndpoints, serveCmd.Flags().Lookup("etcd-endpoints"))
viper.BindPFlag(etcdTimeout, serveCmd.Flags().Lookup("etcd-timeout"))
}
17 changes: 6 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,22 @@ module github.com/redradrat/kable
go 1.14

require (
github.com/99designs/keyring v1.1.6 // indirect
github.com/AlecAivazis/survey/v2 v2.1.1
github.com/coreos/etcd v3.3.10+incompatible // indirect
github.com/coreos/etcd v3.3.10+incompatible
github.com/fatih/color v1.9.0
github.com/ghodss/yaml v1.0.0 // indirect
github.com/fatih/structs v1.1.0
github.com/go-git/go-git/v5 v5.1.0
github.com/google/go-jsonnet v0.16.1-0.20200908152747-b70cbd441a39 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/google/logger v1.1.0
github.com/grafana/tanka v0.12.0
github.com/jsonnet-bundler/jsonnet-bundler v0.4.0
github.com/labstack/echo/v4 v4.1.16
github.com/manifoldco/promptui v0.7.0 // indirect
github.com/labstack/gommon v0.3.0
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.1.2
github.com/olekukonko/tablewriter v0.0.4
github.com/otiai10/copy v1.2.0 // indirect
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.6.1 // indirect
github.com/whilp/git-urls v1.0.0 // indirect
github.com/zalando/go-keyring v0.1.0
github.com/ugorji/go v1.1.5-pre // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
k8s.io/apimachinery v0.19.2 // indirect
)
Loading

0 comments on commit a0b4bce

Please sign in to comment.