-
Notifications
You must be signed in to change notification settings - Fork 0
feat: export fctl commands as json #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Dav-14
wants to merge
1
commit into
main
Choose a base branch
from
feat/export_fctl_command_as_json_
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,131 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/formancehq/fctl/cmd" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
var ( | ||
file = "inventory.json" | ||
) | ||
|
||
type Flag struct { | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
Deprecated string `json:"deprecated,omitempty"` | ||
Usage string `json:"usage"` | ||
DefaultValue string `json:"default_value,omitempty"` | ||
Type string `json:"type,omitempty"` | ||
} | ||
|
||
type DocCommand struct { | ||
Name string `json:"name"` | ||
Usage string `json:"usage"` | ||
Description string `json:"description"` | ||
Deprecated string `json:"deprecated,omitempty"` | ||
Aliases []string `json:"aliases,omitempty"` | ||
|
||
Flags []Flag `json:"flags,omitempty"` | ||
GlobalFlags []Flag `json:"global_flags,omitempty"` | ||
|
||
SubCommands []DocCommand `json:"subcommands,omitempty"` | ||
} | ||
|
||
func flagSetToFlags(flagSet *pflag.FlagSet) []Flag { | ||
flags := make([]Flag, 0) | ||
|
||
flagSet.VisitAll(func(f *pflag.Flag) { | ||
if f.Hidden { | ||
return | ||
} | ||
|
||
userHomeDir, err := os.UserHomeDir() | ||
if err != nil { | ||
panic(err) | ||
} | ||
if strings.Contains(f.DefValue, userHomeDir) { | ||
f.DefValue = strings.ReplaceAll(f.DefValue, userHomeDir, "~") | ||
} | ||
|
||
flags = append(flags, Flag{ | ||
Name: f.Name, | ||
Description: f.Usage, | ||
Deprecated: f.Deprecated, | ||
DefaultValue: f.DefValue, | ||
Usage: f.Usage, | ||
Type: f.Value.Type(), | ||
}) | ||
}) | ||
|
||
return flags | ||
} | ||
|
||
func cobraCommandAsDocCommand(parentCommandName string, command *cobra.Command) DocCommand { | ||
return DocCommand{ | ||
Name: func() string { | ||
if parentCommandName != "" { | ||
return fmt.Sprintf("%s %s", parentCommandName, command.Name()) | ||
} | ||
return command.Name() | ||
}(), | ||
Usage: command.Use, | ||
Description: command.Short, | ||
Deprecated: command.Deprecated, | ||
Aliases: command.Aliases, | ||
Flags: flagSetToFlags(command.Flags()), | ||
GlobalFlags: flagSetToFlags(command.Root().PersistentFlags()), | ||
} | ||
|
||
} | ||
|
||
func getFullCommandPath(cmd *cobra.Command) string { | ||
if cmd == nil || cmd == cmd.Root() { | ||
return "" | ||
} | ||
parent := getFullCommandPath(cmd.Parent()) | ||
if parent == "" { | ||
return cmd.Name() | ||
} | ||
return parent + " " + cmd.Name() | ||
} | ||
|
||
func visitSubCommands(command *cobra.Command) []DocCommand { | ||
parentName := getFullCommandPath(command.Parent()) | ||
docs := []DocCommand{cobraCommandAsDocCommand(parentName, command)} | ||
for _, subCommand := range command.Commands() { | ||
docs = append(docs, visitSubCommands(subCommand)...) | ||
} | ||
return docs | ||
} | ||
|
||
//go:generate rm -rf docs | ||
//go:generate go run ./ | ||
func main() { | ||
_, err := os.Stat(file) | ||
if err == nil { | ||
if err := os.Remove(file); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
commands := visitSubCommands(cmd.NewRootCommand()) | ||
file, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of remove the file with |
||
if err != nil { | ||
panic(err) | ||
} | ||
defer file.Close() | ||
|
||
b, err := json.Marshal(commands) | ||
if err != nil { | ||
panic(err) | ||
} | ||
if _, err := file.Write(b); err != nil { | ||
panic(err) | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why run the binary on generate?