Skip to content

Commit

Permalink
clean up docs and add version
Browse files Browse the repository at this point in the history
  • Loading branch information
tzneal committed Nov 30, 2021
1 parent b49670c commit fb80806
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ builds:
- linux
- windows
- darwin
ldflags:
- -s -w -X github.com/tzneal/supplant/cmd.version={{.Version}} -X github.com/tzneal/supplant/cmd.commit={{.Commit}} -X github.com/tzneal/supplant/cmd.date={{.Date}}
archives:
- replacements:
darwin: Darwin
Expand Down
2 changes: 1 addition & 1 deletion cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// cleanCmd represents the clean command
var cleanCmd = &cobra.Command{
Use: "clean",
Use: "clean [flags] config.yml",
Short: "clean removes all disabled items from a configuration file",
Long: `clean removes all disabled items. The standard workflow
is to use the 'create' commnad to construct a new configuration file
Expand Down
8 changes: 4 additions & 4 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// createCmd represents the create command
var createCmd = &cobra.Command{
Use: "create",
Use: "create [flags] config.yml",
Short: "create constructs a configuration file based on the cluster",
Long: `create constructs a configuration file by looking at the services
in the cluster. This is intended to provide a template to allow
Expand All @@ -34,7 +34,7 @@ easy construction of the configuration.`,
log.Fatalf("error listing services: %s", err)
}
cfg := model.Config{}
includeAll,_ := cmd.Flags().GetBool("all")
includeAll, _ := cmd.Flags().GetBool("all")
for _, svc := range svcList.Items {
// skip kube-system services by default
if skipByDefault(svc) && !includeAll {
Expand All @@ -49,7 +49,7 @@ easy construction of the configuration.`,
}

func skipByDefault(svc v1.Service) bool {
if svc.Namespace =="kube-system" {
if svc.Namespace == "kube-system" {
return true
}
if svc.Namespace == "default" && svc.Name == "kubernetes" {
Expand All @@ -72,5 +72,5 @@ func writeConfig(cfg model.Config, outputFile string) {

func init() {
configCmd.AddCommand(createCmd)
createCmd.Flags().BoolP("all","A",false,"If true, include items in the kube-system namespace")
createCmd.Flags().BoolP("all", "A", false, "If true, include items in the kube-system namespace")
}
4 changes: 2 additions & 2 deletions cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func printInfo(format string, a ...interface{}) {
}

func printError(format string, a ...interface{}) {
color.Red("ERROR " + format, a...)
color.Red("ERROR "+format, a...)
}
func printWarn(format string, a ...interface{}) {
color.Yellow("WARN " + format, a...)
color.Yellow("WARN "+format, a...)
}
22 changes: 15 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

var (
version = "dev"
date = "unknown"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "supplant",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Short: "supplant replaces services in a K8s cluster",
Long: `supplant is used to replace a service in a K8s cluster
and point this service at your local machine. This allows
you to develop/debug a service locally and let it interact
with the rest of the services in a cluster.`,
Version: version,
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -31,4 +37,6 @@ func init() {
pflag.CommandLine = flags
flags.AddFlagSet(rootCmd.PersistentFlags())
kubeConfigFlags.AddFlags(flags)

rootCmd.SetVersionTemplate(fmt.Sprintf("supplant version {{.Version}} / %s\n", date))
}
18 changes: 6 additions & 12 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ import (

// runCmd represents the run command
var runCmd = &cobra.Command{
Use: "run",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Use: "run [flags] config.yml",
Short: "run launches a configuration",
Long: `run launches a configuration, pointing services to local ports
on your machine and forwarding local ports to services
inside the cluster as described by the configuration file.`,
Args: cobra.ExactValidArgs(1),
Run: func(cmd *cobra.Command, args []string) {
inputFile := args[0]
Expand Down Expand Up @@ -119,7 +116,7 @@ to quickly create a Cobra application.`,

// delete the existing service
err = cs.CoreV1().Services(svc.Namespace).Delete(ctx, svc.Name, metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err){
if err != nil && !errors.IsNotFound(err) {
log.Fatalf("error deleting existing service %s: %s", svc.Name, err)
}

Expand Down Expand Up @@ -186,7 +183,6 @@ to quickly create a Cobra application.`,
portForwardingAtLeastOne = true
}


if !supplantingAtLeastOne && !portForwardingAtLeastOne {
printError("no services configured for supplanting or port forwarding, exiting...")
os.Exit(0)
Expand All @@ -213,7 +209,6 @@ to quickly create a Cobra application.`,
// wait on the Ctrl+C
<-signals


printHeader("cleaning up....")
for _, fw := range portForwards {
printList("closing port forward %s:%d", fw.Name, fw.Port)
Expand All @@ -237,7 +232,6 @@ to quickly create a Cobra application.`,
}
}


// and replace our services which will re-create the endpoints based on the selectors
for _, sb := range serviceBackups {
printList("restoring service %s", sb.Name)
Expand Down

0 comments on commit fb80806

Please sign in to comment.