package uid
import (
"net/url"
"os"
"path/filepath"
"strings"
"github.com/carapace-sh/carapace/internal/pflagfork"
"github.com/spf13/cobra"
)
type Context interface {
Abs (s string ) (string , error )
Getenv (key string ) string
LookupEnv (key string ) (string , bool )
}
func Command (cmd *cobra .Command ) *url .URL {
path := []string {cmd .Name ()}
for parent := cmd .Parent (); parent != nil ; parent = parent .Parent () {
path = append (path , parent .Name ())
}
reverse (path )
return &url .URL {
Scheme : "cmd" ,
Host : path [0 ],
Path : strings .Join (path [1 :], "/" ),
}
}
func reverse(s []string ) {
for i , j := 0 , len (s )-1 ; i < j ; i , j = i +1 , j -1 {
s [i ], s [j ] = s [j ], s [i ]
}
}
func Flag (cmd *cobra .Command , flag *pflagfork .Flag ) *url .URL {
uid := Command (cmd )
values := uid .Query ()
values .Set ("flag" , flag .Name )
uid .RawQuery = values .Encode ()
return uid
}
func Executable () string {
executable , err := os .Executable ()
if err != nil {
return "echo"
}
switch base := filepath .Base (executable ); base {
case "cmd.test" :
return "example"
case "ld-musl-x86_64.so.1" :
return filepath .Base (os .Args [0 ])
default :
return base
}
}
func Map (uids ...string ) func (s string ) (*url .URL , error ) {
return func (s string ) (*url .URL , error ) {
for i := 0 ; i < len (uids ); i += 2 {
if uids [i ] == s {
return url .Parse (uids [i +1 ])
}
}
return &url .URL {}, nil
}
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .