Skip to content

Commit

Permalink
Add support to install, remove and search apps (#2)
Browse files Browse the repository at this point in the history
Signed-off-by: Prasad Ghangal <[email protected]>
  • Loading branch information
PrasadG193 authored Feb 9, 2021
1 parent 55e699e commit f850739
Show file tree
Hide file tree
Showing 9 changed files with 587 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.DEFAULT_GOAL := build
.PHONY: build pre-build system-check cli

#Build the binary
build: pre-build
@cd cmd/cli;GOOS_VAL=$(shell go env GOOS) GOARCH_VAL=$(shell go env GOARCH) go build -o $(shell go env GOPATH)/bin/kbrew
@echo "Build completed successfully"

cli:
@cd cmd/cli;GOOS_VAL=$(shell go env GOOS) GOARCH_VAL=$(shell go env GOARCH) go build -o $(shell go env GOPATH)/bin/kbrew
@echo "cli generated successfully"

#system checks
system-check:
@echo "Checking system information"
@if [ -z "$(shell go env GOOS)" ] || [ -z "$(shell go env GOARCH)" ] ; \
then \
echo 'ERROR: Could not determine the system architecture.' && exit 1 ; \
else \
echo 'GOOS: $(shell go env GOOS)' ; \
echo 'GOARCH: $(shell go env GOARCH)' ; \
echo 'System information checks passed.'; \
fi ;

#Pre-build checks
pre-build: system-check
131 changes: 131 additions & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package main

import (
"context"
"fmt"
"os"
"os/exec"
"strings"
"text/tabwriter"

"github.com/spf13/cobra"

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

type method string

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

var (
rootCmd = &cobra.Command{
Use: "kbrew",
Short: "Homebrew for your Kubernetes applications",
Long: `TODO: Long description`,
}

installCmd = &cobra.Command{
Use: "install [NAME]",
Short: "Install application",
RunE: func(cmd *cobra.Command, args []string) error {
return manageApp(create, args)
},
}

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

searchCmd = &cobra.Command{
Use: "search [NAME]",
Short: "Search application",
RunE: func(cmd *cobra.Command, args []string) error {
return search(args)
},
}
)

func init() {
rootCmd.AddCommand(installCmd)
rootCmd.AddCommand(removeCmd)
rootCmd.AddCommand(searchCmd)
}

func main() {
Execute()
}

// Execute executes the main command
func Execute() error {
return rootCmd.Execute()
}

func manageApp(m method, args []string) error {
var app apps.App
var err error
for _, a := range args {
app, err = raw.New(a)
if err != nil {
// TODO: Check other app types
return err
}
data, err := app.Manifest(context.Background(), nil)
if err != nil {
return err
}
//fmt.Printf("Data:: \n%s\n", data)
if err := executeCommand(m, data); err != nil {
return err
}
}
return nil
}

func search(args []string) error {
ctx := context.Background()
// List raw apps
rawApps, err := raw.List(ctx)
if err != nil {
return err
}
//TODO: Support for other app types
if len(args) == 0 {
printList(rawApps)
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) {
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"))
}
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()
}
10 changes: 10 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"fmt"
)

func main() {
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags)
f := cmdutil.NewFactory(matchVersionKubeConfigFlags)
}
23 changes: 23 additions & 0 deletions data/raw/nginx/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx:1.12
imagePullPolicy: IfNotPresent
name: web
ports:
- containerPort: 80
name: http
protocol: TCP

14 changes: 14 additions & 0 deletions data/raw/nginx/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx
spec:
ports:
- name: server
port: 80
protocol: TCP
type: ClusterIP
selector:
app: nginx
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/vishal-biyani/kbrew

go 1.15

require (
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
)
Loading

0 comments on commit f850739

Please sign in to comment.