Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
add some commands
Browse files Browse the repository at this point in the history
  • Loading branch information
iojcde committed Aug 17, 2021
1 parent 9437376 commit 6ffde8e
Show file tree
Hide file tree
Showing 15 changed files with 713 additions and 29 deletions.
15 changes: 15 additions & 0 deletions check/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package check

import (
"os"

"github.com/manifoldco/promptui"
"github.com/spf13/viper"
)

func CheckSignin() {
if viper.Get("key") == nil {
println(promptui.IconBad + (" Please log in using `fhctl login` first.\n"))
os.Exit(0)
}
}
22 changes: 22 additions & 0 deletions check/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package check

import (
"fmt"
"os"
)

func CheckErr(err error, msg string) {
if err != nil {
if err.Error() == "^C" {
os.Exit(0)
}
if msg != "" {
fmt.Printf("%v: %v\n", msg, err)
return
}
if msg == "" {
fmt.Printf("Error: %v\n", err)
return
}
}
}
46 changes: 46 additions & 0 deletions cmd/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright © 2021 JcdeA <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd

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

// aliasCmd represents the alias command
var aliasCmd = &cobra.Command{
Use: "alias",
Short: "Alias resources for convenience",
}

func init() {
rootCmd.AddCommand(aliasCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// aliasCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// aliasCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
95 changes: 95 additions & 0 deletions cmd/aliasProject.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright © 2021 JcdeA <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd

import (
"errors"
"fmt"
"strings"

"github.com/jcdea/fhctl/check"
"github.com/jcdea/fhctl/request"
"github.com/jcdea/fhctl/types"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// aliasProjectCmd represents the aliasProject command
var aliasProjectCmd = &cobra.Command{
Use: "project",
Short: "Alias a project",

Run: func(cmd *cobra.Command, args []string) {
projectResp, err := request.GetProjects()

var projectNames []string
for _, item := range projectResp.Projects {
projectNames = append(projectNames, item.Name)
}
check.CheckErr(err, "")
selectProject := promptui.Select{
Label: "Select project",
Items: projectNames,
}
selectedIndex, selected, err := selectProject.Run()
check.CheckErr(err, "")

validate := func(aliasName string) error {

if strings.Contains(aliasName, ":") {
return errors.New("invalid name")
}
return nil
}
aliasPrompt := promptui.Prompt{
Label: fmt.Sprintf("Name to alias \"%v\" to", selected),
Validate: validate,
}

aliasName, err := aliasPrompt.Run()
check.CheckErr(err, "")

viper.Set("alias."+aliasName, types.AliasItem{
Id: projectResp.Projects[selectedIndex].Id,
Type: types.Project,
})

createOrWriteConfig(0600)
println(promptui.IconGood + " Successfully aliased resource!")

},
}

func init() {
aliasCmd.AddCommand(aliasProjectCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// aliasProjectCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// aliasProjectCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
84 changes: 78 additions & 6 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,95 @@ THE SOFTWARE.
package cmd

import (
"errors"
"fmt"
"net/mail"
"os"

"github.com/jcdea/aarch64-client-go"
"github.com/jcdea/fhctl/check"
"github.com/jcdea/fhctl/spinner"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// loginCmd represents the login command
var loginCmd = &cobra.Command{
Use: "login",
Short: "Login to Fosshost",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("login called")
validate := func(email string) error {
_, err := mail.ParseAddress(email)
if err != nil {
return errors.New("invalid email")
}
return nil
}
if viper.ConfigFileUsed() != "" {
choose := promptui.Select{
Label: fmt.Sprintf("Configuration file found at %v", viper.ConfigFileUsed()),
Items: []string{"use existing configuration", "overwite config file"},
}
i, _, err := choose.Run()
check.CheckErr(err, "")

switch i {
case 0:
println("using existing configuration.")
os.Exit(0)
}

}

emailPrompt := promptui.Prompt{
Label: "Your Email",
Validate: validate,
}
email, err := emailPrompt.Run()
check.CheckErr(err, "Prompt failed")

PwPrompt := promptui.Prompt{
Label: "Password",
Mask: '*',
}
pw, err := PwPrompt.Run()

check.CheckErr(err, "Prompt failed")

s, err := spinner.SpinnerWithMsg(spinner.SpinnerMsgs{Suffix: "Signing in",
SuccessMsg: "Successfully logged in!\n",
FailMsg: "Failed to sign in. Please try again.\n"})
check.CheckErr(err, "")

s.Start()

client := aarch64.NewClient("")

resp, err := client.Login(email, pw)

check.CheckErr(err, "")

if resp.Meta.Success {

s.Stop()
configWriteSpinner, err := spinner.SpinnerWithMsg(spinner.SpinnerMsgs{
Suffix: "Writing configuration file",
SuccessMsg: "Successfully saved configuration file!\n"})
check.CheckErr(err, "")
configWriteSpinner.Start()

viper.Set("email", email)
viper.Set("key", resp.Key)
createOrWriteConfig(0600)

configWriteSpinner.Stop()

} else {
s.StopFail()
}

},
}

Expand Down
46 changes: 46 additions & 0 deletions cmd/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright © 2021 JcdeA <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd

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

// projectCmd represents the project command
var projectCmd = &cobra.Command{
Use: "project",
Short: "Commands for managing projects",
}

func init() {
rootCmd.AddCommand(projectCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// projectCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// projectCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
63 changes: 63 additions & 0 deletions cmd/projectLs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright © 2021 JcdeA <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd

import (
"fmt"

"github.com/jcdea/aarch64-client-go"
"github.com/jcdea/fhctl/check"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// projectLsCmd represents the projectLs command
var projectLsCmd = &cobra.Command{
Use: "ls",
Short: "List projects",

Run: func(cmd *cobra.Command, args []string) {
check.CheckSignin()

client := aarch64.NewClient(viper.GetString("key"))
resp, err := client.Projects()

check.CheckErr(err, "")
for i, item := range resp.Projects {
fmt.Printf("%v: %v\n", i, item.Name)
}
},
}

func init() {
projectCmd.AddCommand(projectLsCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// projectLsCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// projectLsCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
Loading

0 comments on commit 6ffde8e

Please sign in to comment.