diff options
author | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-11-06 10:19:06 +0100 |
---|---|---|
committer | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-11-06 14:49:43 +0100 |
commit | a58e296c780f3f9f3a8f71a27cef0d202b13674b (patch) | |
tree | 72dcd72ca9a8a7c30c7e44338c708a86ab18a8e7 /lib/agent/projects.go | |
parent | be13167b869161b6e19dc3e94835245cdc7911e5 (diff) |
Support short project id name if not ambiguous.
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'lib/agent/projects.go')
-rw-r--r-- | lib/agent/projects.go | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/lib/agent/projects.go b/lib/agent/projects.go index 6804d35..f089882 100644 --- a/lib/agent/projects.go +++ b/lib/agent/projects.go @@ -3,6 +3,7 @@ package agent import ( "fmt" "log" + "strings" "time" "github.com/iotbzh/xds-agent/lib/apiv1" @@ -66,6 +67,27 @@ func (p *Projects) Init(server *XdsServer) error { return nil } +// ResolveID Complete a Project ID (helper for user that can use partial ID value) +func (p *Projects) ResolveID(id string) (string, error) { + if id == "" { + return "", nil + } + + match := []string{} + for iid := range p.projects { + if strings.HasPrefix(iid, id) { + match = append(match, iid) + } + } + + if len(match) == 1 { + return match[0], nil + } else if len(match) == 0 { + return id, fmt.Errorf("Unknown id") + } + return id, fmt.Errorf("Multiple IDs found with provided prefix: " + id) +} + // Get returns the folder config or nil if not existing func (p *Projects) Get(id string) *IPROJECT { if id == "" { @@ -204,7 +226,7 @@ func (p *Projects) Delete(id string) (apiv1.ProjectConfig, error) { fld := apiv1.ProjectConfig{} fc, exist := p.projects[id] if !exist { - return fld, fmt.Errorf("unknown id") + return fld, fmt.Errorf("Unknown id") } prj := (*fc).GetProject() |