Skip to content
This repository has been archived by the owner on Sep 21, 2023. It is now read-only.

Commit

Permalink
Set up basic Cobra structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Patch committed Jul 6, 2017
1 parent 390d44c commit 34660b2
Show file tree
Hide file tree
Showing 381 changed files with 65,949 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export GO15VENDOREXPERIMENT=1
export CGO_ENABLED:=0

PROJ=issue-sync
ORG_PATH=github.com/coreos
REPO_PATH=$(ORG_PATH)/$(PROJ)
VERSION=$(shell ./git-version)
BUILD_TIME=`date +%FT%T%z`
GOOS=$(shell go env GOOS)
GOARCH=$(shell go env GOARCH)
SOURCES := $(shell find . -name '*.go')
LD_FLAGS=-ldflags "-X $(REPO_PATH)/cmd.Version=$(VERSION)"

build: bin/$(PROJ)

bin/$(PROJ): $(SOURCES)
@go build -o bin/$(PROJ) $(LD_FLAGS) $(REPO_PATH)

clean:
@rm bin/*

.PHONY: clean

.DEFAULT_GOAL: build
114 changes: 114 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package cmd

import (
"strings"

"os"

"fmt"

"github.com/Sirupsen/logrus"
"github.com/fsnotify/fsnotify"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
log *logrus.Logger
defaultLogLevel = logrus.InfoLevel
rootCmdFile string
rootCmdCfg *viper.Viper
)

func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

var RootCmd = &cobra.Command{
Use: "issue-sync [options]",
Short: "A tool to synchronize GitHub and JIRA issues",
Long: "Full docs coming later; see https://github.com/coreos/issue-sync",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
rootCmdCfg.BindPFlags(cmd.Flags())
initRootLogger()
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Called root job; not written yet")
},
}

func init() {
log = logrus.New()
cobra.OnInitialize(func() {
rootCmdCfg = newViper("issue-sync", rootCmdFile)
})
RootCmd.PersistentFlags().String("log-level", logrus.InfoLevel.String(), "Set the global log level")
RootCmd.PersistentFlags().StringVar(&rootCmdFile, "config", "", "Config file (default is $HOME/.issue-sync.yaml)")
RootCmd.PersistentFlags().String("github-token", "", "Set the API Token used to access the GitHub repo")
}

func initRootLogger() {
log = logrus.New()

logLevel := parseLogLevel(rootCmdCfg.GetString("log-level"))
log.Level = logLevel
log.WithField("log-level", logLevel).Debug("root log level set")
}

func parseLogLevel(level string) logrus.Level {
if level == "" {
return defaultLogLevel
}

ll, err := logrus.ParseLevel(level)
if err != nil {
fmt.Printf("Failed to parse log level, using default. Error: %v\n", err)
return defaultLogLevel
}
return ll
}

func newViper(appName, cfgFile string) *viper.Viper {
v := viper.New()

v.SetEnvPrefix(appName)
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
v.AutomaticEnv()

v.SetConfigName(fmt.Sprintf("config-%s", appName))
v.AddConfigPath(".")
if cfgFile != "" {
v.SetConfigFile(cfgFile)
}

if err := v.ReadInConfig(); err == nil {
log.WithField("file", v.ConfigFileUsed()).Infof("config file loaded")
v.WatchConfig()
v.OnConfigChange(func(e fsnotify.Event) {
log.WithField("file", e.Name).Info("config file changed")
})
} else {
if cfgFile != "" {
log.WithError(err).Warningf("Error reading config file: %s", cfgFile)
}
}

if log.Level == logrus.DebugLevel {
v.Debug()
}

return v
}

func newLogger(app, level string) *logrus.Entry {
logger := logrus.New()
logger.Level = parseLogLevel(level)
logEntry := logrus.NewEntry(logger).WithFields(logrus.Fields{
"app": app,
})
logEntry.WithField("log-level", logger.Level).Info("log level set")
return logEntry
}
32 changes: 32 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright © 2017 CoreOS Inc
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd

import (
"github.com/spf13/cobra"
)

var Version = "0.1.0"

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "Returns the current version of issue-sync",
Run: func(cmd *cobra.Command, args []string) {
log.Println("issue-sync version ", Version)
},
}

func init() {
RootCmd.AddCommand(versionCmd)
}
21 changes: 21 additions & 0 deletions git-version
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash -e

# pull the current git commit hash
COMMIT=`git rev-parse HEAD`

# check if the current commit has a matching tag
TAG=$(git describe --exact-match --abbrev=0 --tags ${COMMIT} 2> /dev/null || true)

# use the matching tag as the version, if available
if [ -z "$TAG" ]; then
VERSION=$COMMIT
else
VERSION=$TAG
fi

# check for changed files (not untracked files)
if [ -n "$(git diff --shortstat 2> /dev/null | tail -n1)" ]; then
VERSION="${VERSION}_dirty"
fi

echo $VERSION
16 changes: 16 additions & 0 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package: github.com/coreos/issue-sync
import:
- package: github.com/spf13/cobra
- package: github.com/spf13/viper
- package: github.com/Sirupsen/logrus
version: v1.0.0
- package: github.com/google/go-github
- package: golang.org/x/oauth2
- package: github.com/coreos/pkg
version: v3
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/coreos/issue-sync/cmd"

func main() {
cmd.Execute()
}
1 change: 1 addition & 0 deletions vendor/github.com/Sirupsen/logrus/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions vendor/github.com/Sirupsen/logrus/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 100 additions & 0 deletions vendor/github.com/Sirupsen/logrus/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/Sirupsen/logrus/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 34660b2

Please sign in to comment.