Skip to content

Instantly share code, notes, and snippets.

@estesp
Created September 18, 2014 19:21
Show Gist options
  • Save estesp/1e1d3b17d35acf3f9259 to your computer and use it in GitHub Desktop.
Save estesp/1e1d3b17d35acf3f9259 to your computer and use it in GitHub Desktop.
proposal for name vs. ID lookup priority
package main
import (
"fmt"
"flag"
"regexp"
)
var (
fullID = regexp.MustCompile(`^[0-9a-fA-F]{64}$`)
hexID = regexp.MustCompile(`^[0-9a-fA-F]{1,64}$`)
)
func main() {
flag.Parse()
args := flag.Args()
if len(args) < 1 {
fmt.Println("need an ID/name argument")
return
}
id := args[0]
//easiest case: argument is a full ID
if (fullID.MatchString(id)) {
fmt.Println("CHECK BY ID: full 64-digit hex identifier provided")
//do we even want to check a name if the ID lookup fails? what's the probability?
} else {
//not a full ID; check if it is a shortened ID
if (hexID.MatchString(id)) {
fmt.Println("ID matches hex pattern < 64 chars long - could be shortened ID")
// but check names first for exact match ("db" case)
fmt.Println("CHECK NAME - does it exist? then return exact name match")
fmt.Println("NOW CHECK ID - if error return, check not specific enough?")
//any reason to consider: what if name and ID lookup both return a container/image?
} else {
fmt.Println("CHECK NAME ONLY: not 64 len and not even hex")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment