in oss/lib/cli_bridge.go [150:232]
func ParseAndRunCommandFromCli(ctx *cli.Context, args []string) error {
// 利用 parser 解析 flags,否则下文读不到
parser := cli.NewParser(args, ctx)
_, err := parser.ReadAll()
if err != nil {
return err
}
profile, err := config.LoadProfileWithContext(ctx)
if err != nil {
return fmt.Errorf("config failed: %s", err.Error())
}
proxyHost, ok := ctx.Flags().GetValue("proxy-host")
if !ok {
proxyHost = ""
}
credential, err := profile.GetCredential(ctx, tea.String(proxyHost))
if err != nil {
return fmt.Errorf("can't get credential %s", err)
}
model, err := credential.GetCredential()
if err != nil {
return fmt.Errorf("can't get credential %s", err)
}
configs := make(map[string]string, 0)
if model.AccessKeyId != nil {
configs["access-key-id"] = *model.AccessKeyId
}
if model.AccessKeySecret != nil {
configs["access-key-secret"] = *model.AccessKeySecret
}
if model.SecurityToken != nil {
configs["sts-token"] = *model.SecurityToken
}
// read endpoint from flags
endpoint, err := ParseAndGetEndpoint(ctx, args)
if err != nil {
return fmt.Errorf("parse endpoint failed: %s", err)
}
// check use http force
forceUseHttp := ctx.Insecure()
if endpoint != "" && !strings.HasPrefix(endpoint, "http://") && !strings.HasPrefix(endpoint, "https://") {
if forceUseHttp {
endpoint = "http://" + endpoint
} else {
endpoint = "https://" + endpoint
}
}
configs["endpoint"] = endpoint
a2 := []string{"aliyun", "oss"}
a2 = append(a2, ctx.Command().Name)
a2 = append(a2, args...)
configFlagSet := cli.NewFlagSet()
config.AddFlags(configFlagSet)
for _, f := range ctx.Flags().Flags() {
if configFlagSet.Get(f.Name) != nil {
continue
}
if f.IsAssigned() {
a2 = append(a2, "--"+f.Name)
if s2, ok := f.GetValue(); ok && s2 != "" {
a2 = append(a2, s2)
}
}
}
for k, v := range configs {
if v != "" {
a2 = append(a2, "--"+k)
a2 = append(a2, v)
}
}
os.Args = a2[1:]
return ParseAndRunCommand()
}