github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/starlib/resolve.go (about) 1 // Copyright 2022 Edward McFarlane. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package starlib 6 7 import ( 8 "fmt" 9 "net/url" 10 "path" 11 "strings" 12 ) 13 14 const keyParam = "key" 15 16 func getValues(s string) (string, url.Values, error) { 17 vs := strings.SplitN(s, "?", 2) 18 s = vs[0] 19 if len(vs) == 1 { 20 return s, nil, nil 21 } 22 23 vals, err := url.ParseQuery(vs[1]) 24 if err != nil { 25 return "", nil, err 26 } 27 return s, vals, nil 28 } 29 30 // resolveModuleURL creates a blob URL for a module string. 31 // 32 // Thread names are specialised blob urls with the key encoded as a param. 33 // mem://?key=current.star 34 // 35 // TODO: load syntax for git modules... 36 func resolveModuleURL(name string, module string) (bktURL string, pathStr string, err error) { 37 var vals url.Values 38 bktURL, vals, err = getValues(name) 39 if err != nil { 40 return 41 } 42 43 key := vals.Get(keyParam) 44 dir := path.Dir(key) 45 46 if strings.HasPrefix(module, ".") { 47 fmt.Println(dir, module) 48 pathStr = path.Join(".", dir, module) 49 } else { 50 // Resolve what bucket or if its local 51 err = fmt.Errorf("TODO: resolve non-local modules: %s", module) 52 return 53 54 } 55 56 vals.Del(keyParam) 57 if len(vals) > 0 { 58 bktURL += "?" + vals.Encode() 59 } 60 61 return 62 } 63 64 //func setKey(key string) {}