MCPcopy Index your code
hub / github.com/coder/coder / readOrGenerateConfig

Function readOrGenerateConfig

coderd/devtunnel/tunnel.go:85–136  ·  view source on GitHub ↗
(customTunnelHost string)

Source from the content-addressed store, hash-verified

83}
84
85func readOrGenerateConfig(customTunnelHost string) (Config, error) {
86 cfgFi, err := cfgPath()
87 if err != nil {
88 return Config{}, xerrors.Errorf("get config path: %w", err)
89 }
90
91 fi, err := os.ReadFile(cfgFi)
92 if err != nil {
93 if os.IsNotExist(err) {
94 cfg, err := GenerateConfig(customTunnelHost)
95 if err != nil {
96 return Config{}, xerrors.Errorf("generate config: %w", err)
97 }
98
99 err = writeConfig(cfg)
100 if err != nil {
101 return Config{}, xerrors.Errorf("write config: %w", err)
102 }
103
104 return cfg, nil
105 }
106
107 return Config{}, xerrors.Errorf("read config: %w", err)
108 }
109
110 cfg := Config{}
111 err = json.Unmarshal(fi, &cfg)
112 if err != nil {
113 return Config{}, xerrors.Errorf("unmarshal config: %w", err)
114 }
115
116 if cfg.Version == 0 {
117 _, _ = fmt.Println()
118 pretty.Printf(cliui.DefaultStyles.Error, "You're running a deprecated tunnel version.\n")
119 pretty.Printf(cliui.DefaultStyles.Error, "Upgrading you to the new version now. You will need to rebuild running workspaces.")
120 _, _ = fmt.Println()
121
122 cfg, err := GenerateConfig(customTunnelHost)
123 if err != nil {
124 return Config{}, xerrors.Errorf("generate config: %w", err)
125 }
126
127 err = writeConfig(cfg)
128 if err != nil {
129 return Config{}, xerrors.Errorf("write config: %w", err)
130 }
131
132 return cfg, nil
133 }
134
135 return cfg, nil
136}
137
138func GenerateConfig(customTunnelHost string) (Config, error) {
139 priv, err := tunnelsdk.GeneratePrivateKey()

Callers 1

NewFunction · 0.85

Calls 6

cfgPathFunction · 0.85
GenerateConfigFunction · 0.85
writeConfigFunction · 0.70
ReadFileMethod · 0.65
ErrorfMethod · 0.45
UnmarshalMethod · 0.45

Tested by

no test coverage detected