From ebf38065f8a0c0c3d1a3a7ce2a4caaaad712432c Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Mon, 6 Nov 2017 14:50:03 +0100 Subject: Added short project ID name support if not ambiguous. --- lib/apiv1/events.go | 6 +++++- lib/apiv1/exec.go | 14 +++++++++----- lib/apiv1/folders.go | 22 +++++++++++++++++----- lib/apiv1/make.go | 16 ++++++++++------ lib/model/folders.go | 21 +++++++++++++++++++++ 5 files changed, 62 insertions(+), 17 deletions(-) (limited to 'lib') diff --git a/lib/apiv1/events.go b/lib/apiv1/events.go index 8f92ae0..9444262 100644 --- a/lib/apiv1/events.go +++ b/lib/apiv1/events.go @@ -122,11 +122,15 @@ func (s *APIService) eventsRegister(c *gin.Context) { data := make(folder.EventCBData) data["sid"] = sess.ID - err := s.mfolders.RegisterEventChange(args.ProjectID, &cbFunc, &data) + prjID, err := s.mfolders.ResolveID(args.ProjectID) if err != nil { common.APIError(c, err.Error()) return } + if err = s.mfolders.RegisterEventChange(prjID, &cbFunc, &data); err != nil { + common.APIError(c, err.Error()) + return + } c.JSON(http.StatusOK, gin.H{"status": "OK"}) } diff --git a/lib/apiv1/exec.go b/lib/apiv1/exec.go index 0167196..8ffad07 100644 --- a/lib/apiv1/exec.go +++ b/lib/apiv1/exec.go @@ -105,15 +105,19 @@ func (s *APIService) execCmd(c *gin.Context) { } // Allow to pass id in url (/exec/:id) or as JSON argument - id := c.Param("id") - if id == "" { - id = args.ID + idArg := c.Param("id") + if idArg == "" { + idArg = args.ID } - if id == "" { + if idArg == "" { common.APIError(c, "Invalid id") return } - + id, err := s.mfolders.ResolveID(idArg) + if err != nil { + common.APIError(c, err.Error()) + return + } f := s.mfolders.Get(id) if f == nil { common.APIError(c, "Unknown id") diff --git a/lib/apiv1/folders.go b/lib/apiv1/folders.go index a231b86..398e21c 100644 --- a/lib/apiv1/folders.go +++ b/lib/apiv1/folders.go @@ -16,7 +16,12 @@ func (s *APIService) getFolders(c *gin.Context) { // getFolder returns a specific folder configuration func (s *APIService) getFolder(c *gin.Context) { - f := s.mfolders.Get(c.Param("id")) + id, err := s.mfolders.ResolveID(c.Param("id")) + if err != nil { + common.APIError(c, err.Error()) + return + } + f := s.mfolders.Get(id) if f == nil { common.APIError(c, "Invalid id") return @@ -67,11 +72,14 @@ func (s *APIService) addFolder(c *gin.Context) { // syncFolder force synchronization of folder files func (s *APIService) syncFolder(c *gin.Context) { - id := c.Param("id") - + id, err := s.mfolders.ResolveID(c.Param("id")) + if err != nil { + common.APIError(c, err.Error()) + return + } s.log.Debugln("Sync folder id: ", id) - err := s.mfolders.ForceSync(id) + err = s.mfolders.ForceSync(id) if err != nil { common.APIError(c, err.Error()) return @@ -82,7 +90,11 @@ func (s *APIService) syncFolder(c *gin.Context) { // delFolder deletes folder from server config func (s *APIService) delFolder(c *gin.Context) { - id := c.Param("id") + id, err := s.mfolders.ResolveID(c.Param("id")) + if err != nil { + common.APIError(c, err.Error()) + return + } s.log.Debugln("Delete folder id ", id) diff --git a/lib/apiv1/make.go b/lib/apiv1/make.go index 223d4bf..6e0c7d6 100644 --- a/lib/apiv1/make.go +++ b/lib/apiv1/make.go @@ -68,15 +68,19 @@ func (s *APIService) buildMake(c *gin.Context) { } // Allow to pass id in url (/make/:id) or as JSON argument - id := c.Param("id") - if id == "" { - id = args.ID + idArg := c.Param("id") + if idArg == "" { + idArg = args.ID } - if id == "" { + if idArg == "" { common.APIError(c, "Invalid id") return } - + id, err := s.mfolders.ResolveID(idArg) + if err != nil { + common.APIError(c, err.Error()) + return + } pf := s.mfolders.Get(id) if pf == nil { common.APIError(c, "Unknown id") @@ -197,7 +201,7 @@ func (s *APIService) buildMake(c *gin.Context) { data["RootPath"] = prj.RootPath data["ExitImmediate"] = args.ExitImmediate - err := common.ExecPipeWs(cmd, args.Env, sop, sess.ID, args.CmdID, execTmo, s.log, oCB, eCB, &data) + err = common.ExecPipeWs(cmd, args.Env, sop, sess.ID, args.CmdID, execTmo, s.log, oCB, eCB, &data) if err != nil { common.APIError(c, err.Error()) return diff --git a/lib/model/folders.go b/lib/model/folders.go index d5ea3bf..b8e6cf5 100644 --- a/lib/model/folders.go +++ b/lib/model/folders.go @@ -146,6 +146,27 @@ func (f *Folders) SaveConfig() error { return foldersConfigWrite(f.fileOnDisk, f.getConfigArrUnsafe()) } +// ResolveID Complete a Folder ID (helper for user that can use partial ID value) +func (f *Folders) ResolveID(id string) (string, error) { + if id == "" { + return "", nil + } + + match := []string{} + for iid := range f.folders { + 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 (f *Folders) Get(id string) *folder.IFOLDER { if id == "" { -- cgit 1.2.3-korg