MCPcopy
hub / github.com/cloudflare/cloudflared / FindOrCreateConfigPath

Function FindOrCreateConfigPath

config/configuration.go:123–152  ·  view source on GitHub ↗

FindOrCreateConfigPath returns the first path that contains a config file or creates one in the primary default path if it doesn't exist

()

Source from the content-addressed store, hash-verified

121// FindOrCreateConfigPath returns the first path that contains a config file
122// or creates one in the primary default path if it doesn't exist
123func FindOrCreateConfigPath() string {
124 path := FindDefaultConfigPath()
125
126 if path == "" {
127 // create the default directory if it doesn't exist
128 path = DefaultConfigPath()
129 if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
130 return ""
131 }
132
133 // write a new config file out
134 file, err := os.Create(path)
135 if err != nil {
136 return ""
137 }
138 defer file.Close()
139
140 logDir := DefaultLogDirectory()
141 _ = os.MkdirAll(logDir, os.ModePerm) // try and create it. Doesn't matter if it succeed or not, only byproduct will be no logs
142
143 c := Root{
144 LogDirectory: logDir,
145 }
146 if err := yaml.NewEncoder(file).Encode(&c); err != nil {
147 return ""
148 }
149 }
150
151 return path
152}
153
154// ValidateUnixSocket ensures --unix-socket param is used exclusively
155// i.e. it fails if a user specifies both --url and --unix-socket

Callers 1

handleServiceModeFunction · 0.92

Calls 5

FindDefaultConfigPathFunction · 0.85
DefaultConfigPathFunction · 0.85
DefaultLogDirectoryFunction · 0.85
CloseMethod · 0.65
EncodeMethod · 0.45

Tested by

no test coverage detected