Skip to content

Commit

Permalink
Created a Go CLI project skeleton.
Browse files Browse the repository at this point in the history
  • Loading branch information
artob committed Mar 22, 2019
1 parent 66f8c5e commit e36f522
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions drycop/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Executable binary
drycop
62 changes: 62 additions & 0 deletions drycop/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* This is free and unencumbered software released into the public domain. */

package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var configFile string
var debug bool
var verbose bool

// RootCmd describes the `drycop` command
var RootCmd = &cobra.Command{
Use: "drycop",
Short: "DRYcop",
Long: `DRYcop is a lint tool for DRY projects.`,
Version: "0.0.0",
}

// Execute implements the `drycop` command
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func init() {
cobra.OnInitialize(initConfig)
RootCmd.PersistentFlags().StringVarP(&configFile, "config", "C", "", "Set config file (default: $HOME/.drycop/config.yaml)")
RootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "Enable debugging")
RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Be verbose")
RootCmd.SetVersionTemplate(`DRYcop {{printf "%s" .Version}}
`)
}

// initConfig reads the configuration file and environment variables.
func initConfig() {
if configFile != "" {
// Use the specified config file:
viper.SetConfigFile(configFile)
} else {
// Search for config file in the current directory and under the home directory:
viper.SetConfigName("config")
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/.drycop")
}

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in:
if err := viper.ReadInConfig(); err == nil {
if debug {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
}
10 changes: 10 additions & 0 deletions drycop/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* This is free and unencumbered software released into the public domain. */

// drycop is a lint tool for DRY projects.
package main

import "github.com/dryproject/drycop/drycop/cmd"

func main() {
cmd.Execute()
}

0 comments on commit e36f522

Please sign in to comment.