package vset import ( "fmt" "os" "strings" ) // The Path interface contains two functions: Validate() and Update(). // Validate() validates the input path from Start(). // Once validated, the path is passed to Update() to check if the .vscode directory and settings.json file exist. // If either of those items don't exist, Update() creates and adds them to the path. // The updated path is returned to Start(). type Path interface { Update(path string) string Validate(path string) } func Update(path string) (string, error) { // Assert that the path ends with a /. if !strings.HasSuffix(path, "/") { path = path + "/" } if err := Validate(path); err != nil { return "", err } goLintPath = path path = path + ".vscode" // Check if .vscode directory exists and create it if it doesn't. _, err := os.Stat(path) if os.IsNotExist(err) { err = os.MkdirAll(path, 0755) if err != nil { return "", fmt.Errorf("failed to create .vscode dir: %w", err) } } path = path + "/settings.json" _, err = os.ReadFile(path) // If a user does not have a settings.json file, an empty one is created for configuration. if err != nil { err = os.WriteFile(path, []byte(`{}`), 0644) if err != nil { return "", fmt.Errorf("failed to create settings.json: %w", err) } } return path, nil } func Validate(path string) error { // Validate the path. _, err := os.Stat(path) if os.IsNotExist(err) { return fmt.Errorf("invalid path: %w", err) } return nil }