lazy config parsing when there's a known client already. The client c may be nil.
()
| 82 | // lazy config parsing when there's a known client already. |
| 83 | // The client c may be nil. |
| 84 | func (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) |
no test coverage detected