package main import ("fmt" "os" ) func checkFileExists(path string) bool { _, err := os.Open(path) if err != nil { return false } return true } func createFile(path string) bool { f, err := os.Create(path) if err != nil { return false; } defer f.Close() fmt.Printf("[FILE] created `%s`\n", f.Name()) return true; } func removeFile(path string) bool { err := os.Remove(path) if err != nil { // panic(err) return false; } fmt.Printf("[FILE] removed `%s`\n", path) return true } func file(m interface{}) error { data, ok := m.(map[string]interface{}) if !ok { return fmt.Errorf("expected map[string]interface{}, got %T", m) } shouldPresent := false if data["state"] == "absent" { shouldPresent = false } else if data["state"] == "present"{ shouldPresent = true } path := data["path"] if checkFileExists(path.(string)){ if shouldPresent { // Skip fmt.Printf("[FILE] %s exists SKIPPING\n", path) } else { // Remove File removeFile(path.(string)) } } else { if shouldPresent { // Create File createFile(path.(string)) } else { // Skip fmt.Printf("[FILE] %s doesn't exist SKIPPING\n", path) } } return nil }