Skip to content

Commit

Permalink
Merge pull request #2 from mtslzr/feature/authCommand
Browse files Browse the repository at this point in the history
Add new command for "auth"
  • Loading branch information
mtslzr committed Jan 24, 2021
2 parents 81e3f7e + e2e19f1 commit 634968e
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 5 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Expand Up @@ -18,4 +18,7 @@
.idea

# Binary
bin/*
bin/*

# macOS
.DS_Store
6 changes: 5 additions & 1 deletion go.mod
Expand Up @@ -2,4 +2,8 @@ module github.com/mtslzr/dio

go 1.16

require github.com/spf13/cobra v1.1.1
require (
github.com/sirupsen/logrus v1.2.0
github.com/spf13/cobra v1.1.1
gopkg.in/yaml.v2 v2.2.8
)
11 changes: 11 additions & 0 deletions go.sum
Expand Up @@ -32,6 +32,7 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
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=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
Expand Down Expand Up @@ -97,10 +98,13 @@ github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfV
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
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 @@ -123,6 +127,7 @@ github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FI
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
Expand All @@ -140,6 +145,7 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
Expand All @@ -157,6 +163,7 @@ github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5q
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
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=
Expand All @@ -171,6 +178,7 @@ golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 h1:58fnuSXlxZmFdJyvtTFVmVhcMLU6v5fEb/ok4wyqtNU=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -224,6 +232,7 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0 h1:HyfiK1WMnHj5FXFXatD+Qs1A/xC2Run6RzeW1SyHxpc=
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down Expand Up @@ -271,13 +280,15 @@ 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.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
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=
Expand Down
72 changes: 72 additions & 0 deletions src/cmd/auth.go
@@ -0,0 +1,72 @@
package cmd

import (
"github.com/mtslzr/dio/src/pkg/auth"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)

const (
flagTokenDesc = "personal access token for Github"
flagUserDesc = "username for Github"
)

type Flags struct {
Destroy bool `json:"destroy"`
Status bool `json:"status"`
Token string `json:"token"`
User string `json:"user"`
}

var (
flags = Flags{}

authCmd = &cobra.Command{
Use: "auth",
Short: "Authenticate Dio with Github.",
Run: func(cmd *cobra.Command, args []string) {
err := auth.Authenticate(flags.Token, flags.User)
if err != nil {
returnResult(err)
}

returnResult(auth.Status())
},
}

authDelCmd = &cobra.Command{
Use: "delete",
Short: "Delete existing token configuration.",
Run: func(cmd *cobra.Command, arg []string) {
returnResult(auth.Destroy())
},
}

authStatCmd = &cobra.Command{
Use: "status",
Short: "Check existing token configuration.",
Run: func(cmd *cobra.Command, arg []string) {
returnResult(auth.Status())
},
}
)

func init() {
authCmd.Flags().StringVarP(&flags.Token, "token", "t", "", flagTokenDesc)
authCmd.Flags().StringVarP(&flags.User, "user", "u", "", flagUserDesc)
authCmd.MarkFlagRequired("token")
authCmd.MarkFlagRequired("user")

authCmd.AddCommand(authDelCmd)
authCmd.AddCommand(authStatCmd)
rootCmd.AddCommand(authCmd)
}

func returnResult(err error) {
if err != nil {
log.Errorf("Encountered errors while processing request.")
os.Exit(1)
}
os.Exit(0)
}
19 changes: 19 additions & 0 deletions src/cmd/new.go
@@ -0,0 +1,19 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

var newCmd = &cobra.Command{
Use: "new",
Short: "Create new project using Dio.",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("New project with Dio!")
},
}

func init() {
rootCmd.AddCommand(newCmd)
}
9 changes: 6 additions & 3 deletions src/cmd/root.go
Expand Up @@ -4,15 +4,18 @@ import (
"fmt"
"os"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "dio",
Short: "Command-line tool to bootstrap new projects.",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Dio!")
},
}

func init() {
log.SetOutput(os.Stdout)
log.SetLevel(log.WarnLevel)
}

// Executes runs the root command.
Expand Down
23 changes: 23 additions & 0 deletions src/pkg/auth/auth.go
@@ -0,0 +1,23 @@
package auth

import (
log "github.com/sirupsen/logrus"
)

// Authenticate writes a new personal token to config.
func Authenticate(token string, user string) error {
log.Info("Setting up local config file...")
return createConfig(token, user)
}

// Destroy removes existing token configuration.
func Destroy() error {
log.Info("Removing existing token configuration...")
return destroyConfig()
}

// Status gets the current status of token configuration.
func Status() error {
log.Info("Checking current token configuration...")
return checkConfig()
}
123 changes: 123 additions & 0 deletions src/pkg/auth/config.go
@@ -0,0 +1,123 @@
package auth

import (
"fmt"
"io/ioutil"
"os"
"os/user"

log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)

const (
configDir = ".config/dio"
configFile = "token.yml"
configTemplateDir = "src/pkg/auth" // TODO: Fix this to work with standalone binary.

msgConfigDelete = "Local configuration and token deleted."
msgNoConfigFile = "No token currently configured."
)

type ConfigYaml struct {
Github struct {
User string `json:"user"`
Token string `json:"token"`
} `json:"github.com"`
}

func checkConfig() error {
homeDir, err := getHomeDir()
if err != nil {
return err
}

cfg, err := ioutil.ReadFile(fmt.Sprintf("%s/%s/%s", homeDir, configDir, configFile))
if cfg == nil {
log.Infof("No configuration file found.")
fmt.Println(msgNoConfigFile)
return nil
}
if err != nil {
log.Errorf("Error reading config file: %+v", err)
return err
}

fmt.Println(string(cfg))
return nil
}

func createConfig(token string, user string) error {
homeDir, err := getHomeDir()
if err != nil {
return err
}

err = os.MkdirAll(fmt.Sprintf("%s/%s", homeDir, configDir), 0755)
if err != nil {
log.Errorf("Error creating config directory: %+v", err)
return err
}

data, err := parseYaml()
if err != nil {
return err
}

data.Github.User = user
data.Github.Token = token
dataYaml, err := yaml.Marshal(data)
if err != nil {
log.Errorf("Error marshaling YAML: %+v", err)
return err
}

err = ioutil.WriteFile(fmt.Sprintf("%s/%s/%s", homeDir, configDir, configFile), dataYaml, 0600)
if err != nil {
log.Errorf("Error writing config file: %+v", err)
}

return err
}

func destroyConfig() error {
homeDir, err := getHomeDir()
if err != nil {
return err
}

err = os.RemoveAll(fmt.Sprintf("%s/%s", homeDir, configDir))
if err != nil {
log.Errorf("Error deleting config folder: %+v", err)
return err
}

fmt.Println(msgConfigDelete)
return nil
}

func getHomeDir() (string, error) {
usr, err := user.Current()
if err != nil {
log.Errorf("Error getting current home directory: %+v", err)
}

return usr.HomeDir, err
}

func parseYaml() (ConfigYaml, error) {
var data ConfigYaml

ymlFile, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", configTemplateDir, configFile))
if err != nil {
log.Errorf("Error reading config template: %+v", err)
return data, err
}

err = yaml.Unmarshal(ymlFile, &data)
if err != nil {
log.Errorf("Error unmarshaling YAML: %+v", err)
}

return data, err
}
3 changes: 3 additions & 0 deletions src/pkg/auth/token.yml
@@ -0,0 +1,3 @@
github.com:
user: USERNAME
token: TOKEN

0 comments on commit 634968e

Please sign in to comment.