-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement removing user from config based on name, only w specified c…
…onfig path
- Loading branch information
Showing
2 changed files
with
85 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 |
---|---|---|
@@ -1,37 +1,82 @@ | ||
package offboard | ||
|
||
import ( | ||
"fmt" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/open-sauced/pizza-cli/v2/pkg/config" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
type Options struct { | ||
offboardingUsers []string | ||
|
||
// config file path | ||
configPath string | ||
|
||
// CODEOWNERS file path | ||
ownersPath string | ||
|
||
// from global config | ||
ttyDisabled bool | ||
} | ||
|
||
const configLongDesc string = `[WIP] Removes a user from the \".sauced.yaml\" config and \"CODEOWNERS\" files. | ||
const offboardLongDesc string = `[WIP] Removes a user from the \".sauced.yaml\" config and \"CODEOWNERS\" files. | ||
Requires the user's name OR email.` | ||
|
||
func NewConfigCommand() *cobra.Command { | ||
options := &Options{} | ||
opts := &Options{} | ||
cmd := &cobra.Command{ | ||
Use: "offboard <username/email> [flags]", | ||
Short: "[WIP] Removes a user from the \".sauced.yaml\" config and \"CODEOWNERS\" files.", | ||
Long: configLongDesc, | ||
Long: offboardLongDesc, | ||
Args: func(_ *cobra.Command, args []string) error { | ||
if !(len(args) > 0) { | ||
errors.New("you must provide at least one argument: the offboarding user's email/username") | ||
} | ||
|
||
options.offboardingUsers = args | ||
opts.offboardingUsers = args | ||
|
||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
opts.ttyDisabled, _ = cmd.Flags().GetBool("tty-disable") | ||
opts.configPath, _ = cmd.Flags().GetString("config") | ||
|
||
opts.ownersPath, _ = cmd.Flags().GetString("owners-path") | ||
err := run(opts) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
cmd.PersistentFlags().StringP("owners-path", "o", "./CODEOWNERS", "the CODEOWNERS or OWNERS file to update") | ||
return cmd | ||
} | ||
|
||
func run(opts *Options) error { | ||
// read config spec | ||
spec, _, err := config.LoadConfig(opts.configPath) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
attributions := spec.Attributions | ||
for _, user := range opts.offboardingUsers { | ||
// deletes if the user is a name (key) | ||
delete(attributions, user) | ||
} | ||
|
||
fmt.Print(attributions) | ||
|
||
err = generateOutputFile(opts.configPath, attributions) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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,35 @@ | ||
package offboard | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/open-sauced/pizza-cli/v2/pkg/config" | ||
"github.com/open-sauced/pizza-cli/v2/pkg/utils" | ||
) | ||
|
||
func generateOutputFile(outputPath string, attributionMap map[string][]string) error { | ||
file, err := os.Create(outputPath) | ||
if err != nil { | ||
return fmt.Errorf("error creating %s file: %w", outputPath, err) | ||
} | ||
defer file.Close() | ||
|
||
var config config.Spec | ||
config.Attributions = attributionMap | ||
|
||
// for pretty print test | ||
yaml, err := utils.OutputYAML(config) | ||
|
||
if err != nil { | ||
return fmt.Errorf("failed to turn into YAML: %w", err) | ||
} | ||
|
||
_, err = file.WriteString(yaml) | ||
|
||
if err != nil { | ||
return fmt.Errorf("failed to turn into YAML: %w", err) | ||
} | ||
|
||
return nil | ||
} |