MCPcopy
hub / github.com/perkeep/perkeep / parseConfig

Method parseConfig

pkg/client/config.go:84–162  ·  view source on GitHub ↗

lazy config parsing when there's a known client already. The client c may be nil.

()

Source from the content-addressed store, hash-verified

82// lazy config parsing when there's a known client already.
83// The client c may be nil.
84func (c *Client) parseConfig() {
85 if android.OnAndroid() {
86 panic("parseConfig should never have been called on Android")
87 }
88 if configDisabled {
89 panic("parseConfig should never have been called with CAMLI_DISABLE_CLIENT_CONFIG_FILE set")
90 }
91 configPath := osutil.UserClientConfigPath()
92 if _, err := wkfs.Stat(configPath); os.IsNotExist(err) {
93 if c != nil && c.isSharePrefix {
94 return
95 }
96 errMsg := fmt.Sprintf("Client configuration file %v does not exist. See 'pk-put init' to generate it.", configPath)
97 if keyID := serverKeyId(); keyID != "" {
98 hint := fmt.Sprintf("\nThe key id %v was found in the server config %v, so you might want:\n'pk-put init -gpgkey %v'", keyID, osutil.UserServerConfigPath(), keyID)
99 errMsg += hint
100 }
101 log.Fatal(errMsg)
102 }
103 // TODO: instead of using jsonconfig, we could read the file,
104 // and unmarshal into the structs that we now have in
105 // pkg/types/clientconfig. But we'll have to add the old
106 // fields (before the name changes, and before the
107 // multi-servers change) to the structs as well for our
108 // graceful conversion/error messages to work.
109 conf, err := osutil.NewJSONConfigParser().ReadFile(configPath)
110 if err != nil {
111 log.Fatal(err.Error())
112 }
113 cfg := jsonconfig.Obj(conf)
114
115 if singleServerAuth := cfg.OptionalString("auth", ""); singleServerAuth != "" {
116 newConf, err := convertToMultiServers(cfg)
117 if err != nil {
118 log.Print(err)
119 } else {
120 cfg = newConf
121 }
122 }
123
124 config = &clientconfig.Config{
125 Identity: cfg.OptionalString("identity", ""),
126 IdentitySecretRing: cfg.OptionalString("identitySecretRing", ""),
127 IgnoredFiles: cfg.OptionalList("ignoredFiles"),
128 }
129 serversList := make(map[string]*clientconfig.Server)
130 servers := cfg.OptionalObject("servers")
131 for alias, vei := range servers {
132 // An alias should never be confused with a host name,
133 // so we forbid anything looking like one.
134 if isURLOrHostPort(alias) {
135 log.Fatalf("Server alias %q looks like a hostname; \".\" or \";\" are not allowed.", alias)
136 }
137 serverMap, ok := vei.(map[string]interface{})
138 if !ok {
139 log.Fatalf("entry %q in servers section is a %T, want an object", alias, vei)
140 }
141 serverConf := jsonconfig.Obj(serverMap)

Callers 1

parseConfigFunction · 0.95

Calls 13

OnAndroidFunction · 0.92
UserClientConfigPathFunction · 0.92
UserServerConfigPathFunction · 0.92
NewJSONConfigParserFunction · 0.92
serverKeyIdFunction · 0.85
convertToMultiServersFunction · 0.85
isURLOrHostPortFunction · 0.85
cleanServerFunction · 0.85
printConfigChangeHelpFunction · 0.85
FatalMethod · 0.80
StatMethod · 0.65
FatalfMethod · 0.65

Tested by

no test coverage detected