1
0
Fork 0
mirror of https://github.com/Luzifer/password.git synced 2024-11-08 17:30:10 +00:00

Replaced codegangsta/cli by spf13/cobra

This commit is contained in:
Knut Ahlers 2015-05-29 20:20:04 +02:00
parent cc8b722429
commit da4aefee63
5 changed files with 158 additions and 106 deletions

37
cmdGet.go Normal file
View file

@ -0,0 +1,37 @@
package main
import (
"fmt"
"os"
"github.com/Luzifer/password/lib"
"github.com/spf13/cobra"
)
func getCmdGet() *cobra.Command {
cmd := cobra.Command{
Use: "get",
Short: "generate and return a secure random password",
Run: actionCmdGet,
}
cmd.Flags().IntVarP(&flags.CLI.Length, "length", "l", 20, "length of the generated password")
cmd.Flags().BoolVarP(&flags.CLI.SpecialCharacters, "special", "s", false, "use special characters in your password")
return &cmd
}
func actionCmdGet(cmd *cobra.Command, args []string) {
password, err := pwd.GeneratePassword(flags.CLI.Length, flags.CLI.SpecialCharacters)
if err != nil {
switch {
case err == securepassword.ErrLengthTooLow:
fmt.Println("The password has to be more than 4 characters long to meet the security considerations")
default:
fmt.Println("An unknown error occured")
}
os.Exit(1)
}
fmt.Println(password)
}

72
cmdServe.go Normal file
View file

@ -0,0 +1,72 @@
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
)
func getCmdServe() *cobra.Command {
cmd := cobra.Command{
Use: "serve",
Short: "start an API server to request passwords",
Run: actionCmdServe,
}
cmd.Flags().IntVar(&flags.Server.Port, "port", 3000, "port to listen on")
return &cmd
}
func actionCmdServe(cmd *cobra.Command, args []string) {
r := mux.NewRouter()
r.HandleFunc("/", handleFrontend).Methods("GET")
r.PathPrefix("/assets").HandlerFunc(handleAssets).Methods("GET")
r.HandleFunc("/v1/getPassword", handleAPIGetPasswordv1).Methods("GET")
http.Handle("/", r)
http.ListenAndServe(fmt.Sprintf(":%d", flags.Server.Port), nil)
}
func handleAPIGetPasswordv1(res http.ResponseWriter, r *http.Request) {
length, err := strconv.Atoi(r.URL.Query().Get("length"))
if err != nil {
length = 20
}
special := r.URL.Query().Get("special") == "true"
if length > 128 || length < 4 {
http.Error(res, "Please do not use length with more than 128 or fewer than 4 characters!", http.StatusNotAcceptable)
return
}
password, err := pwd.GeneratePassword(length, special)
res.Header().Add("Content-Type", "text/plain")
res.Header().Add("Cache-Control", "no-cache")
res.Write([]byte(password))
}
func handleFrontend(res http.ResponseWriter, r *http.Request) {
res.Header().Add("Content-Type", "text/html")
buf, err := Asset("frontend/index.html")
if err != nil {
http.Error(res, "Unable to load interface", http.StatusInternalServerError)
log.Println(err)
return
}
res.Write(buf)
}
func handleAssets(res http.ResponseWriter, r *http.Request) {
buf, err := Asset(fmt.Sprintf("frontend%s", r.URL.Path))
if err != nil {
http.Error(res, "Unable to load interface", http.StatusInternalServerError)
return
}
res.Write(buf)
}

22
cmdVersion.go Normal file
View file

@ -0,0 +1,22 @@
package main
import (
"fmt"
"github.com/spf13/cobra"
)
func getCmdVersion() *cobra.Command {
cmd := cobra.Command{
Use: "version",
Aliases: []string{"v"},
Short: "prints the current version of awsenv",
Run: actionCmdVersion,
}
return &cmd
}
func actionCmdVersion(cmd *cobra.Command, args []string) {
fmt.Printf("password version %s\n", version)
}

12
flags.go Normal file
View file

@ -0,0 +1,12 @@
package main
var flags = struct {
CLI struct {
Length int
SpecialCharacters bool
}
Server struct {
Port int
}
}{}

121
main.go
View file

@ -1,15 +1,12 @@
package main package main
import ( import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/Luzifer/password/lib" "github.com/Luzifer/password/lib"
"github.com/codegangsta/cli" "github.com/spf13/cobra"
"github.com/gorilla/mux" )
const (
version = "1.3.0"
) )
var pwd *securepassword.SecurePassword var pwd *securepassword.SecurePassword
@ -19,104 +16,16 @@ func init() {
} }
func main() { func main() {
app := cli.NewApp() rootCmd := cobra.Command{
app.Usage = "generates secure random passwords" Use: "password",
app.Version = "1.0.0" Short: "generates secure random passwords",
app.Commands = []cli.Command{
{
Name: "serve",
Usage: "start an API server to request passwords",
Action: startAPIServer,
Flags: []cli.Flag{
cli.IntFlag{
Name: "port",
Value: 3000,
Usage: "port to listen on",
},
},
},
{
Name: "get",
Usage: "generate and return a secure random password",
Action: printPassword,
Flags: []cli.Flag{
cli.IntFlag{
Name: "length, l",
Value: 20,
Usage: "length of the generated password",
},
cli.BoolFlag{
Name: "special, s",
Usage: "use special characters in your password",
},
},
},
} }
app.Run(os.Args) rootCmd.AddCommand(
} getCmdGet(),
getCmdServe(),
func startAPIServer(c *cli.Context) { getCmdVersion(),
r := mux.NewRouter() )
r.HandleFunc("/", handleFrontend).Methods("GET")
r.PathPrefix("/assets").HandlerFunc(handleAssets).Methods("GET") rootCmd.Execute()
r.HandleFunc("/v1/getPassword", handleAPIGetPasswordv1).Methods("GET")
http.Handle("/", r)
http.ListenAndServe(fmt.Sprintf(":%d", c.Int("port")), nil)
}
func printPassword(c *cli.Context) {
password, err := pwd.GeneratePassword(c.Int("length"), c.Bool("special"))
if err != nil {
switch {
case err == securepassword.ErrLengthTooLow:
fmt.Println("The password has to be more than 4 characters long to meet the security considerations")
default:
fmt.Println("An unknown error occured")
}
os.Exit(1)
}
fmt.Println(password)
}
func handleAPIGetPasswordv1(res http.ResponseWriter, r *http.Request) {
length, err := strconv.Atoi(r.URL.Query().Get("length"))
if err != nil {
length = 20
}
special := r.URL.Query().Get("special") == "true"
if length > 128 || length < 4 {
http.Error(res, "Please do not use length with more than 128 or fewer than 4 characters!", http.StatusNotAcceptable)
return
}
password, err := pwd.GeneratePassword(length, special)
res.Header().Add("Content-Type", "text/plain")
res.Header().Add("Cache-Control", "no-cache")
res.Write([]byte(password))
}
func handleFrontend(res http.ResponseWriter, r *http.Request) {
res.Header().Add("Content-Type", "text/html")
buf, err := Asset("frontend/index.html")
if err != nil {
http.Error(res, "Unable to load interface", http.StatusInternalServerError)
log.Println(err)
return
}
res.Write(buf)
}
func handleAssets(res http.ResponseWriter, r *http.Request) {
buf, err := Asset(fmt.Sprintf("frontend%s", r.URL.Path))
if err != nil {
http.Error(res, "Unable to load interface", http.StatusInternalServerError)
return
}
res.Write(buf)
} }