Skip to content

Commit

Permalink
fix: minor improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
beneiltis committed Oct 25, 2023
1 parent 4dbb025 commit 2cefd60
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 33 deletions.
9 changes: 7 additions & 2 deletions cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ var cleanCmd = &cobra.Command{
This can be used if something went wrong during automatic setup/cleanup.`,
Run: func(cmd *cobra.Command, args []string) {
yellow := color.New(color.FgYellow).SprintFunc()
if !utils.ConfirmTask(fmt.Sprintf("Do you really want to remove punq from '%s' context?", yellow(kubernetes.CurrentContextName())), 1) {
if !utils.ConfirmTask(fmt.Sprintf("Do you really want to remove punq from '%s' context?", yellow(kubernetes.CurrentContextName()))) {
os.Exit(0)
}
kubernetes.Remove(yellow(kubernetes.CurrentContextName()))

clusterName := kubernetes.CurrentContextName()

kubernetes.Remove(yellow(clusterName))
services.RemoveKeyPair()

fmt.Printf("\n🚀🚀🚀 Successfully uninstalled punq from '%s'.\n\n", clusterName)
},
}

Expand Down
9 changes: 7 additions & 2 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ var installCmd = &cobra.Command{
Please run cleanup if you want to remove it again.`,
Run: func(cmd *cobra.Command, args []string) {
yellow := color.New(color.FgYellow).SprintFunc()
if !utils.ConfirmTask(fmt.Sprintf("Do you really want to install punq to '%s' context?", yellow(kubernetes.CurrentContextName())), 1) {
if !utils.ConfirmTask(fmt.Sprintf("Do you really want to install punq to '%s' context?", yellow(kubernetes.CurrentContextName()))) {
os.Exit(0)
}

kubernetes.Deploy(kubernetes.CurrentContextName(), ingressHostname)
clusterName := kubernetes.CurrentContextName()

kubernetes.Deploy(clusterName, ingressHostname)
services.InitUserService()
services.InitAuthService()
services.CreateAdminUser()

fmt.Printf("\n🚀🚀🚀 Successfully installed punq in '%s'.\n\n", clusterName)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var proxyCmd = &cobra.Command{
Port-Forward to punq within your currently selected kubernetes context.`,
Run: func(cmd *cobra.Command, args []string) {
yellow := color.New(color.FgYellow).SprintFunc()
if !utils.ConfirmTask(fmt.Sprintf("Do you really want to connect to punq in '%s' context?", yellow(kubernetes.CurrentContextName())), 1) {
if !utils.ConfirmTask(fmt.Sprintf("Do you really want to connect to punq in '%s' context?", yellow(kubernetes.CurrentContextName()))) {
os.Exit(0)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var resetConfig = &cobra.Command{
This can be used if something went wrong during automatic cleanup.`,
Run: func(cmd *cobra.Command, args []string) {
yellow := color.New(color.FgYellow).SprintFunc()
if !utils.ConfirmTask(yellow("Do you really want to reset your configuration file to default?"), 1) {
if !utils.ConfirmTask(yellow("Do you really want to reset your configuration file to default?")) {
os.Exit(0)
}
utils.DeleteCurrentConfig()
Expand Down
2 changes: 1 addition & 1 deletion cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var updateOperatorImageCmd = &cobra.Command{
return
} else {
yellow := color.New(color.FgYellow).SprintFunc()
if !utils.ConfirmTask(fmt.Sprintf("Do you really want to upgrade to image '%s' in context '%s'?", yellow(vers), yellow(kubernetes.CurrentContextName())), 1) {
if !utils.ConfirmTask(fmt.Sprintf("Do you really want to upgrade to image '%s' in context '%s'?", yellow(vers), yellow(kubernetes.CurrentContextName()))) {
os.Exit(0)
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/mogenius/punq/dtos"
"github.com/mogenius/punq/services"
"github.com/mogenius/punq/structs"
"github.com/mogenius/punq/utils"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -62,12 +61,13 @@ var addUserCmd = &cobra.Command{
AccessLevel: selectedAccess,
}

_, err := services.AddUser(newUser)
newPunqUser, err := services.AddUser(newUser)
if err != nil {
utils.FatalError(err.Error())
} else {
utils.PrintInfo("User added succesfully ✅.")
structs.PrettyPrint(newUser)
newPunqUser.Password = password // print password to terminal once
newPunqUser.PrintToTerminalWithPwd()
}
},
}
Expand Down Expand Up @@ -136,7 +136,7 @@ var getUserCmd = &cobra.Command{
if err != nil {
utils.FatalError(err.Error())
}
structs.PrettyPrint(user)
user.PrintToTerminal()
},
}

Expand Down
21 changes: 20 additions & 1 deletion dtos/punq-access.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import "strings"
type AccessLevel int

const (
READER AccessLevel = iota
UNKNOWNACCESS AccessLevel = iota
READER
USER
ADMIN
// and so on...
Expand All @@ -34,6 +35,24 @@ func AccessLevelFromString(level string) AccessLevel {
}
}

func (level *AccessLevel) String() string {
if level == nil {
return "UNKNOWNACCESS"
} else {
level := *level
switch level {
case READER:
return "READER"
case USER:
return "USER"
case ADMIN:
return "ADMIN"
default:
return "UNKNOWNACCESS"
}
}
}

// func ListAccess(groups []PunqUser, showPasswords bool) {
// t := table.NewWriter()
// t.SetOutputMirror(os.Stdout)
Expand Down
20 changes: 20 additions & 0 deletions dtos/punq-user.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ func ListUsers(users []PunqUser) {
t.Render()
}

func (user *PunqUser) PrintToTerminal() {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"ID", "DisplayName", "Email", "AccessLevel"})
t.AppendRow(
table.Row{user.Id, user.DisplayName, user.Email, user.AccessLevel.String()},
)
t.Render()
}

func (user *PunqUser) PrintToTerminalWithPwd() {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"ID", "DisplayName", "Email", "Password", "AccessLevel"})
t.AppendRow(
table.Row{user.Id, user.DisplayName, user.Email, user.Password, user.AccessLevel.String()},
)
t.Render()
}

func (user *PunqUser) PasswordCheck(password string) (bool, error) {
err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))

Expand Down
2 changes: 1 addition & 1 deletion kubernetes/addResources.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Deploy(clusterName string, ingressHostname string) {
addIngress(provider, clusterName, ingressHostname)
}

fmt.Printf("\n🚀🚀🚀 Successfully installed punq in '%s'.\n\n", clusterName)
fmt.Printf("\nSuccessfully installed punq in '%s'.\n\n", clusterName)
}

func addService(provider *KubeProvider) {
Expand Down
2 changes: 0 additions & 2 deletions kubernetes/deleteResources.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ func Remove(clusterName string) {
removeUsersSecret(provider)
removeService(provider)
removeIngress(provider)

fmt.Printf("\n🚀🚀🚀 Successfully uninstalled punq from '%s'.\n\n", clusterName)
}

func removeDeployment(provider *KubeProvider) {
Expand Down
7 changes: 3 additions & 4 deletions services/user-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/gin-gonic/gin"
"github.com/mogenius/punq/structs"

"golang.org/x/crypto/bcrypt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -24,7 +23,6 @@ const PunqAdminIdKey = "admin_id"

func InitUserService() {
CreateUserSecret()
CreateAdminUser()
}

func CreateUserSecret() {
Expand All @@ -49,7 +47,7 @@ func CreateUserSecret() {
logger.Log.Error(err)
return
}
fmt.Println("Created new punq-users secret. ✅")
fmt.Println("Created new punq-auth secret. ✅")
}
}

Expand All @@ -75,7 +73,8 @@ func CreateAdminUser() {
// display admin user
displayAdminUser := adminUser
displayAdminUser.Password = password
structs.PrettyPrint(displayAdminUser)
utils.PrintInfo("Please store following admin user credentials in a safe place:")
displayAdminUser.PrintToTerminalWithPwd()

secret = kubernetes.SecretFor(utils.CONFIG.Kubernetes.OwnNamespace, utils.USERSSECRET, nil)
strData := make(map[string]string)
Expand Down
24 changes: 10 additions & 14 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,26 +241,22 @@ func OpenBrowser(url string) {
}
}

func ConfirmTask(s string, tries int) bool {
func ConfirmTask(s string) bool {
r := bufio.NewReader(os.Stdin)

for ; tries > 0; tries-- {
fmt.Printf("%s [y/n]: ", s)
fmt.Printf("%s [Y/n]: ", s)

res, err := r.ReadString('\n')
if err != nil {
log.Fatal(err)
}

// Empty input (i.e. "\n")
if len(res) < 2 {
continue
}
res, err := r.ReadString('\n')
if err != nil {
log.Fatal(err)
}

return strings.ToLower(strings.TrimSpace(res))[0] == 'y'
// Empty input (i.e. "\n")
if res == "\n" {
return true
}

return false
return strings.ToLower(strings.TrimSpace(res)) == "y"
}

func HashString(data string) string {
Expand Down

0 comments on commit 2cefd60

Please sign in to comment.