68 lines
1 KiB
Go
68 lines
1 KiB
Go
package vcs
|
|
|
|
import (
|
|
"encoding"
|
|
"flag"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
_ flag.Value = (*Type)(nil)
|
|
_ encoding.TextUnmarshaler = (*Type)(nil)
|
|
vcsTypes = map[string]Type{
|
|
"git": TypeGit,
|
|
}
|
|
vcsDir = map[Type]string{
|
|
TypeGit: ".git",
|
|
}
|
|
)
|
|
|
|
const (
|
|
TypeGit Type = "git"
|
|
)
|
|
|
|
func ParseType(s string) (Type, error) {
|
|
var t Type
|
|
if err := t.UnmarshalText([]byte(s)); err != nil {
|
|
return "", err
|
|
} else {
|
|
return t, nil
|
|
}
|
|
}
|
|
|
|
type Type string
|
|
|
|
func (t *Type) UnmarshalText(text []byte) error {
|
|
if knownType, ok := vcsTypes[string(text)]; !ok {
|
|
return fmt.Errorf("unknown VCS type %q", t)
|
|
} else {
|
|
*t = knownType
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (t *Type) String() string {
|
|
s := string(*t)
|
|
return s
|
|
}
|
|
|
|
func (t *Type) Set(s string) error {
|
|
if match, ok := vcsTypes[strings.ToLower(s)]; ok {
|
|
*t = match
|
|
return nil
|
|
}
|
|
|
|
return fmt.Errorf("no such VCS type: %s", s)
|
|
}
|
|
|
|
func (t *Type) Type() string {
|
|
return "string"
|
|
}
|
|
|
|
func (t *Type) Directory() string {
|
|
tVal := *t
|
|
return vcsDir[tVal]
|
|
}
|