parseFlagValue ensures that a flag value with state gets resolved This happens most commonly when argument is the result of command expansion from a sub-shell.
(ctx context.Context, value string, arg *modFunctionArg)
| 818 | // This happens most commonly when argument is the result of command expansion |
| 819 | // from a sub-shell. |
| 820 | func (h *shellCallHandler) parseFlagValue(ctx context.Context, value string, arg *modFunctionArg) (rval string, bypass bool, rerr error) { |
| 821 | argType := arg.TypeDef |
| 822 | states := FindStateTokens(value) |
| 823 | |
| 824 | if len(states) == 0 { |
| 825 | if argType.AsObject != nil { |
| 826 | switch argType.AsObject.Name { |
| 827 | case Directory, File: |
| 828 | // Ignore on git urls since the flag will parse it directly. |
| 829 | // We just need to resolve the ref for "local" (contextual) paths. |
| 830 | if _, err := gitutil.ParseURL(value); err != nil { |
| 831 | if newPath, err := h.contextArgRef(value); err == nil { |
| 832 | return newPath, false, nil |
| 833 | } |
| 834 | } |
| 835 | default: |
| 836 | // If the object type doesn't have custom flag support, |
| 837 | // a plain string can't be used — it needs an object ID |
| 838 | // from a constructor (e.g. via command substitution). |
| 839 | if GetCustomFlagValue(argType.AsObject.Name) == nil { |
| 840 | return "", false, fmt.Errorf( |
| 841 | "argument %q expects a %s object, not a string literal; "+ |
| 842 | "use a constructor via command substitution, e.g. $(%s ...)", |
| 843 | arg.FlagName(), |
| 844 | argType.AsObject.Name, |
| 845 | cliName(argType.AsObject.Name), |
| 846 | ) |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | if argType.AsInput != nil { |
| 851 | if GetCustomFlagValue(argType.AsInput.Name) == nil { |
| 852 | return "", false, fmt.Errorf( |
| 853 | "argument %q expects a %s input object, not a string literal; "+ |
| 854 | "use a constructor via command substitution, e.g. $(%s ...)", |
| 855 | arg.FlagName(), |
| 856 | argType.AsInput.Name, |
| 857 | cliName(argType.AsInput.Name), |
| 858 | ) |
| 859 | } |
| 860 | } |
| 861 | return value, false, nil |
| 862 | } |
| 863 | |
| 864 | // If value isn't one state exactly, we need to process into a string |
| 865 | if len(states) > 1 || states[0] != value { |
| 866 | r, err := h.resolveResult(ctx, value) |
| 867 | return r, false, err |
| 868 | } |
| 869 | |
| 870 | // Otherwise it may be an object that we want to bypass (for its ID) |
| 871 | st, err := h.state.Extract(ctx, GetStateKey(value)) |
| 872 | if err != nil { |
| 873 | return "", false, err |
| 874 | } |
| 875 | r, err := h.StateResult(ctx, st) |
| 876 | if err != nil { |
| 877 | return "", false, err |
no test coverage detected