mirror of
https://github.com/daeuniverse/dae.git
synced 2025-07-04 15:27:55 +07:00
feat: support overwrite node name using tag
This commit is contained in:
@ -8,6 +8,7 @@ package dialer
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type FromLinkCreator func(gOption *GlobalOption, iOption InstanceOption, link string) (dialer *Dialer, err error)
|
||||
@ -19,12 +20,35 @@ func FromLinkRegister(name string, creator FromLinkCreator) {
|
||||
}
|
||||
|
||||
func NewFromLink(gOption *GlobalOption, iOption InstanceOption, link string) (dialer *Dialer, err error) {
|
||||
/// Get overwritten name.
|
||||
var overwrittenName string
|
||||
iColon := strings.Index(link, ":")
|
||||
if iColon == -1 {
|
||||
goto parseUrl
|
||||
}
|
||||
// If first colon is like "://" in "scheme://linkbody", no tag is present.
|
||||
if strings.HasPrefix(link[iColon:], "://") {
|
||||
goto parseUrl
|
||||
}
|
||||
// Else tag is the part before colon.
|
||||
overwrittenName = link[:iColon]
|
||||
link = link[iColon+1:]
|
||||
|
||||
parseUrl:
|
||||
u, err := url.Parse(link)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if creator, ok := fromLinkCreators[u.Scheme]; ok {
|
||||
return creator(gOption, iOption, link)
|
||||
node, err := creator(gOption, iOption, link)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Overwrite node name using user given tag.
|
||||
if overwrittenName != "" {
|
||||
node.name = overwrittenName
|
||||
}
|
||||
return node, err
|
||||
} else {
|
||||
return nil, fmt.Errorf("unexpected link type: %v", u.Scheme)
|
||||
}
|
||||
|
Reference in New Issue
Block a user