package cmdenv import ( "fmt" "path/filepath" "strconv" "strings" "gitlab.dms3.io/dms3/go-dms3/commands" "gitlab.dms3.io/dms3/go-dms3/core" "gitlab.dms3.io/dms3/go-dms3/repo/fsrepo" cmds "gitlab.dms3.io/dms3/go-dms3-cmds" config "gitlab.dms3.io/dms3/go-dms3-config" logging "gitlab.dms3.io/dms3/go-log" coreiface "gitlab.dms3.io/dms3/interface-go-dms3-core" options "gitlab.dms3.io/dms3/interface-go-dms3-core/options" idxconfig "gitlab.dms3.io/is/go-idx-config" ) var log = logging.Logger("core/commands/cmdenv") // GetNode extracts the node from the environment. func GetNode(env interface{}) (*core.Dms3Node, error) { ctx, ok := env.(*commands.Context) if !ok { return nil, fmt.Errorf("expected env to be of type %T, got %T", ctx, env) } return ctx.GetNode() } // GetApi extracts CoreAPI instance from the environment. func GetApi(env cmds.Environment, req *cmds.Request) (coreiface.CoreAPI, error) { ctx, ok := env.(*commands.Context) if !ok { return nil, fmt.Errorf("expected env to be of type %T, got %T", ctx, env) } offline, _ := req.Options["offline"].(bool) if !offline { offline, _ = req.Options["local"].(bool) if offline { log.Errorf("Command '%s', --local is deprecated, use --offline instead", strings.Join(req.Path, " ")) } } api, err := ctx.GetAPI() if err != nil { return nil, err } if offline { return api.WithOptions(options.Api.Offline(offline)) } return api, nil } // GetConfig extracts the config from the environment. func GetConfig(env cmds.Environment) (*config.Config, error) { ctx, ok := env.(*commands.Context) if !ok { return nil, fmt.Errorf("expected env to be of type %T, got %T", ctx, env) } return ctx.GetConfig() } // GetIdxConfig extracts the index config from the environment. func GetIdxConfig(env cmds.Environment) (*idxconfig.IdxConfig, error) { ctx, ok := env.(*commands.Context) if !ok { return nil, fmt.Errorf("expected env to be of type %T, got %T", ctx, env) } return ctx.GetIdxConfig() } // GetConfigRoot extracts the config root from the environment func GetConfigRoot(env cmds.Environment) (string, error) { ctx, ok := env.(*commands.Context) if !ok { return "", fmt.Errorf("expected env to be of type %T, got %T", ctx, env) } return ctx.ConfigRoot, nil } // GetIdxConfigRoot extracts the index config root from the environment func GetIdxConfigRoot(env cmds.Environment) (string, error) { cfgRoot, err := GetConfigRoot(env) if err != nil { return "", err } return filepath.Join(cfgRoot, fsrepo.IdxPath), nil } // EscNonPrint converts non-printable characters and backslash into Go escape // sequences. This is done to display all characters in a string, including // those that would otherwise not be displayed or have an undesirable effect on // the display. func EscNonPrint(s string) string { if !needEscape(s) { return s } esc := strconv.Quote(s) // Remove first and last quote, and unescape quotes. return strings.ReplaceAll(esc[1:len(esc)-1], `\"`, `"`) } func needEscape(s string) bool { if strings.ContainsRune(s, '\\') { return true } for _, r := range s { if !strconv.IsPrint(r) { return true } } return false }