// Package uid provides unique identifiers
package uid import ( ) type Context interface { Abs(s string) (string, error) Getenv(key string) string LookupEnv(key string) (string, bool) } // Command creates a uid for given command. func ( *cobra.Command) *url.URL { := []string{.Name()} for := .Parent(); != nil; = .Parent() { = append(, .Name()) } reverse() // TODO slices.Reverse return &url.URL{ Scheme: "cmd", Host: [0], Path: strings.Join([1:], "/"), } } // reverse reverses the elements of the slice in place. func reverse( []string) { for , := 0, len()-1; < ; , = +1, -1 { [], [] = [], [] } } // Flag creates a uid for given flag. func ( *cobra.Command, *pflagfork.Flag) *url.URL { := Command() := .Query() .Set("flag", .Name) .RawQuery = .Encode() return } // Executable returns the name of the executable. func () string { , := os.Executable() if != nil { return "echo" // safe fallback that should never happen } switch := filepath.Base(); { case "cmd.test": return "example" // for `go test -v ./...` case "ld-musl-x86_64.so.1": return filepath.Base(os.Args[0]) // alpine container workaround (gcompat) default: return } } // Map maps values to uids to simplify testing. // // Map( // "go.mod", "file://path/to/go.mod", // "go.sum", "file://path/to/go.sum", // ) func ( ...string) func( string) (*url.URL, error) { return func( string) (*url.URL, error) { for := 0; < len(); += 2 { if [] == { return url.Parse([+1]) } } return &url.URL{}, nil } }