Skip to content

Commit

Permalink
Add support to pass and parse config (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
PrasadG193 authored Feb 17, 2021
1 parent f850739 commit 343a239
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 105 deletions.
119 changes: 77 additions & 42 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,33 @@ package main
import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"text/tabwriter"

homedir "github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/vishal-biyani/kbrew/pkg/apps"
"github.com/vishal-biyani/kbrew/pkg/apps/helm"
"github.com/vishal-biyani/kbrew/pkg/apps/raw"
"github.com/vishal-biyani/kbrew/pkg/config"
)

type method string

const (
create method = "create"
delete method = "delete"
install method = "create"
uninstall method = "uninstall"
)

var (
configFile string
namespace string

rootCmd = &cobra.Command{
Use: "kbrew",
Short: "Homebrew for your Kubernetes applications",
Expand All @@ -31,16 +39,18 @@ var (
installCmd = &cobra.Command{
Use: "install [NAME]",
Short: "Install application",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return manageApp(create, args)
return manageApp(install, args)
},
}

removeCmd = &cobra.Command{
Use: "remove [NAME]",
Short: "Remove application",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return manageApp(delete, args)
return manageApp(uninstall, args)
},
}

Expand All @@ -54,6 +64,10 @@ var (
)

func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&configFile, "config", "", "config file (default is $HOME/.kbrew.yaml)")
rootCmd.PersistentFlags().StringVarP(&namespace, "namespace", "n", "default", "namespace")

rootCmd.AddCommand(installCmd)
rootCmd.AddCommand(removeCmd)
rootCmd.AddCommand(searchCmd)
Expand All @@ -68,64 +82,85 @@ func Execute() error {
return rootCmd.Execute()
}

func checkArgs(args []string) error {
if len(args) == 0 {
errors.New("No app name provided.")
}
return nil
}

func manageApp(m method, args []string) error {
var app apps.App
var err error
ctx := context.Background()
c, err := config.New(configFile)
if err != nil {
return nil
}

for _, a := range args {
app, err = raw.New(a)
if err != nil {
// TODO: Check other app types
return err
var app apps.App
// Check if entry exists in config
if c.App.Name != strings.ToLower(a) {
continue
}
data, err := app.Manifest(context.Background(), nil)
if err != nil {
return err

switch c.App.Type {
case config.Helm:
app = helm.New(c.App, namespace)
case config.Raw:
app = raw.New(c.App, namespace)
default:
return errors.New(fmt.Sprintf("Unsupported app type %s", c.App.Type))
}
//fmt.Printf("Data:: \n%s\n", data)
if err := executeCommand(m, data); err != nil {
return err

switch m {
case install:
return app.Install(ctx, nil)
case uninstall:
return app.Uninstall(ctx)
default:
return errors.New(fmt.Sprintf("Unsupported method %s", m))
}

}
return nil
}

func search(args []string) error {
ctx := context.Background()
// List raw apps
rawApps, err := raw.List(ctx)
// List from config
c, err := config.New(configFile)
if err != nil {
return err
}
//TODO: Support for other app types
if len(args) == 0 {
printList(rawApps)

if len(args) == 0 || strings.HasPrefix(c.App.Name, args[0]) {
printList(c.App)
return nil
}
result := []string{}
for _, a := range rawApps {
if strings.HasPrefix(a, args[0]) {
result = append(result, a)
}
}
printList(result)
return nil
}

func printList(list []string) {
func printList(app config.App) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent)
fmt.Fprintln(w, "NAME\tVERSION")
for _, l := range list {
fmt.Fprintln(w, fmt.Sprintf("%s\t%s", l, "NA"))
}
fmt.Fprintln(w, "NAME\tVERSION\tTYPE")
fmt.Fprintln(w, fmt.Sprintf("%s\t%s\t%s", app.Name, app.Version, app.Type))
w.Flush()

}

func executeCommand(m method, data []byte) error {
// Generate code
c := exec.Command("kubectl", string(m), "-f", "-")
c.Stdin = strings.NewReader(string(data))
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return c.Run()
func initConfig() {
if configFile != "" {
return
}
// Find home directory.
home, err := homedir.Dir()
cobra.CheckErr(err)

// Generate default config file path
configFile = filepath.Join(home, ".kbrew.yaml")
if _, err := os.Stat(configFile); os.IsNotExist(err) {
// Create file with default config
c := []byte("apiVersion: v1\nkind: kbrew\napps:\n")
err := ioutil.WriteFile(configFile, c, 0644)
cobra.CheckErr(err)
}
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/vishal-biyani/kbrew
go 1.15

require (
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.1.1
github.com/urfave/cli v1.22.5
github.com/urfave/cli/v2 v2.3.0
github.com/spf13/cobra v1.1.3
gopkg.in/yaml.v2 v2.4.0
)
20 changes: 9 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -91,6 +89,7 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
Expand All @@ -101,8 +100,10 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
Expand All @@ -111,6 +112,7 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg=
Expand Down Expand Up @@ -153,8 +155,8 @@ github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4k
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v1.1.1 h1:KfztREH0tPxJJ+geloSLaAkaPkr4ki2Er5quFV1TDo4=
github.com/spf13/cobra v1.1.1/go.mod h1:WnodtKOvamDL/PwE2M4iKs8aMDBZ5Q5klgD3qfVJQMI=
github.com/spf13/cobra v1.1.3 h1:xghbfqPkxzxP3C/f3n5DdpAbdKLj4ZE4BWQI362l53M=
github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand All @@ -166,10 +168,6 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU=
github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
Expand Down Expand Up @@ -281,16 +279,16 @@ google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiq
google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
11 changes: 10 additions & 1 deletion pkg/apps/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,14 @@ import (
)

type App interface {
Manifest(ctx context.Context, opt map[string]string) ([]byte, error)
Install(ctx context.Context, opt map[string]string) error
Uninstall(ctx context.Context) error
}

type BaseApp struct {
URL string
Name string
Namespace string
Digest string
Version string
}
63 changes: 63 additions & 0 deletions pkg/apps/helm/helm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package helm

import (
"context"
"os"
"os/exec"

"github.com/vishal-biyani/kbrew/pkg/apps"
"github.com/vishal-biyani/kbrew/pkg/config"
)

type method string

const (
install method = "install"
uninstall method = "delete"
upgrade method = "upgrade"
)

type HelmApp struct {
apps.BaseApp
}

func New(c config.App, namespace string) *HelmApp {
return &HelmApp{
apps.BaseApp{
Name: c.Name,
Namespace: namespace,
Version: c.Version,
URL: c.URL,
Digest: c.SHA256,
},
}
}

func (ha *HelmApp) Install(ctx context.Context, options map[string]string) error {
//TODO: Resolve Deps
// Validate and install chart
// TODO(@prasad): Use go sdks
return helmCommand(install, ha.Name, ha.Namespace, ha.URL)
}

func (ha *HelmApp) Uninstall(ctx context.Context) error {
//TODO: Resolve Deps
// Validate and install chart
// TODO(@prasad): Use go sdks
return helmCommand(uninstall, ha.Name, ha.Namespace, "")
}

func (ha *HelmApp) Manifest(ctx context.Context, opt map[string]string) ([]byte, error) {
return nil, nil
}

func helmCommand(m method, name, namespace, url string) error {
// Needs helm3
c := exec.Command("helm", string(m), name, "--namespace", namespace, url)
if url == "" {
c = exec.Command("helm", string(m), name, "--namespace", namespace)
}
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return c.Run()
}
Loading

0 comments on commit 343a239

Please sign in to comment.