Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from mtslzr/feature/authCommand
Add new command for "auth"
- Loading branch information
Showing
9 changed files
with
266 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,7 @@ | |
.idea | ||
|
||
# Binary | ||
bin/* | ||
bin/* | ||
|
||
# macOS | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
github.com: | ||
user: USERNAME | ||
token: TOKEN |