This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4733b4d
commit 0b7a429
Showing
1 changed file
with
80 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package argsvalidator | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/martinnirtl/dockma/internal/config" | ||
"github.com/martinnirtl/dockma/internal/utils" | ||
"github.com/martinnirtl/dockma/internal/utils/helpers" | ||
"github.com/spf13/cobra" | ||
"github.com/ttacon/chalk" | ||
) | ||
|
||
// OnlyEnvs checks args for valid envs | ||
func OnlyEnvs(cmd *cobra.Command, args []string) error { | ||
for _, arg := range args { | ||
if !utils.Includes(config.GetEnvNames(), arg) { | ||
return fmt.Errorf("No such environment %s", chalk.Underline.TextStyle(arg)) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// OptionalEnv checks for one optional env | ||
func OptionalEnv(cmd *cobra.Command, args []string) error { | ||
if len(args) > 1 { | ||
return fmt.Errorf("Expected 0 or 1 argument. Got %d", len(args)) | ||
} | ||
|
||
for _, arg := range args { | ||
if !utils.Includes(config.GetEnvNames(), arg) { | ||
return fmt.Errorf("No such environment %s", chalk.Underline.TextStyle(arg)) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// OnlyProfiles checks args for valid profiles of active environment | ||
func OnlyProfiles(cmd *cobra.Command, args []string) error { | ||
activeEnv := config.GetActiveEnv() | ||
|
||
for _, arg := range args { | ||
if !utils.Includes(activeEnv.GetProfileNames(), arg) { | ||
return fmt.Errorf("No such environment %s", chalk.Underline.TextStyle(arg)) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// OptionalProfile checks for one optional profile of active environment | ||
func OptionalProfile(cmd *cobra.Command, args []string) error { | ||
if len(args) > 1 { | ||
return fmt.Errorf("Expected 0 or 1 argument. Got %d", len(args)) | ||
} | ||
|
||
activeEnv := config.GetActiveEnv() | ||
|
||
for _, arg := range args { | ||
if !utils.Includes(activeEnv.GetProfileNames(), arg) { | ||
return fmt.Errorf("No such environment %s", chalk.Underline.TextStyle(arg)) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// OnlyServices checks args for valid services of active environment | ||
func OnlyServices(cmd *cobra.Command, args []string) error { | ||
activeEnv := config.GetActiveEnv() | ||
|
||
for _, arg := range args { | ||
if !utils.Includes(helpers.GetEnvServices(activeEnv), arg) { | ||
return fmt.Errorf("No such environment %s", chalk.Underline.TextStyle(arg)) | ||
} | ||
} | ||
|
||
return nil | ||
} |