|
| 1 | +// Copyright (C) 2017-present Arctic Ice Studio <[email protected]> |
| 2 | +// Copyright (C) 2017-present Sven Greb <[email protected]> |
| 3 | +// |
| 4 | +// Project: snowsaw |
| 5 | +// Repository: https://github.com/arcticicestudio/snowsaw |
| 6 | +// License: MIT |
| 7 | + |
| 8 | +// Author: Arctic Ice Studio <[email protected]> |
| 9 | +// Author: Sven Greb <[email protected]> |
| 10 | +// Since: 0.4.0 |
| 11 | + |
| 12 | +// Package clean provides a task runner implementation check for broken symbolic links and automatically remove them if |
| 13 | +// they point to the snowblock directory. |
| 14 | +package clean |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "io/ioutil" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/fatih/color" |
| 24 | + |
| 25 | + "github.com/arcticicestudio/snowsaw/pkg/api/snowblock" |
| 26 | + "github.com/arcticicestudio/snowsaw/pkg/prt" |
| 27 | + "github.com/arcticicestudio/snowsaw/pkg/util/filesystem" |
| 28 | +) |
| 29 | + |
| 30 | +// Clean is a task runner check for broken symbolic links and automatically remove them if they point to the snowblock |
| 31 | +// directory. |
| 32 | +type Clean struct { |
| 33 | + absPaths []string |
| 34 | + targets []*target |
| 35 | + snowblockAbsPath string |
| 36 | +} |
| 37 | + |
| 38 | +type target struct { |
| 39 | + absPath string |
| 40 | + isSymlink bool |
| 41 | + nodeInfo os.FileInfo |
| 42 | + path string |
| 43 | +} |
| 44 | + |
| 45 | +// GetTaskName returns the name of the task this runner can process. |
| 46 | +func (c Clean) GetTaskName() string { |
| 47 | + return "clean" |
| 48 | +} |
| 49 | + |
| 50 | +// Run processes a task using the given task instructions. |
| 51 | +// The snowblockAbsPath parameter is the absolute path of the snowblock used as contextual information. |
| 52 | +func (c *Clean) Run(configuration snowblock.TaskConfiguration, snowblockAbsPath string) error { |
| 53 | + c.snowblockAbsPath = snowblockAbsPath |
| 54 | + |
| 55 | + // Try to assert the type of the given task configurations and process the paths if all values are converted |
| 56 | + // successfully. |
| 57 | + switch configType := configuration.(type) { |
| 58 | + // Handle the only support JSON data structure of type `array` that stores `string` values by converting |
| 59 | + // the given values to strings. |
| 60 | + case []interface{}: |
| 61 | + c.absPaths = []string{} |
| 62 | + c.targets = []*target{} |
| 63 | + for _, value := range configType { |
| 64 | + path, converted := value.(string) |
| 65 | + if !converted { |
| 66 | + prt.Debugf("Invalid clean configuration value %s of type %s", |
| 67 | + color.CyanString("%s", value), color.RedString("%T", value)) |
| 68 | + return fmt.Errorf("invalid clean configuration value: %s", color.RedString("%s", value)) |
| 69 | + } |
| 70 | + // Expand environment variables and special characters in the target paths,... |
| 71 | + expPath, expPathErr := filesystem.ExpandPath(path) |
| 72 | + if expPathErr != nil { |
| 73 | + return fmt.Errorf("could not expand target path %s: %v", color.CyanString(path), expPathErr) |
| 74 | + } |
| 75 | + var absPath string |
| 76 | + // ...ensure relative paths are dissolved from to absolute paths... |
| 77 | + if !filepath.IsAbs(expPath) { |
| 78 | + relToAbsPath, relToAbsPathErr := filepath.Abs(filepath.Join(c.snowblockAbsPath, expPath)) |
| 79 | + if relToAbsPathErr != nil { |
| 80 | + return fmt.Errorf("could not dissolve clean target path relative to snowblock path: %v", relToAbsPathErr) |
| 81 | + } |
| 82 | + absPath = relToAbsPath |
| 83 | + } else { |
| 84 | + dissolvedPath, dissolvePathErr := filepath.Abs(expPath) |
| 85 | + if dissolvePathErr != nil { |
| 86 | + return fmt.Errorf("could not dissolve absolute clean target path: %v", dissolvePathErr) |
| 87 | + } |
| 88 | + absPath = dissolvedPath |
| 89 | + } |
| 90 | + c.absPaths = append(c.absPaths, absPath) |
| 91 | + } |
| 92 | + // ...and deduplicate possible duplicates to prevent to process and traverse same paths multiple times. |
| 93 | + prt.Debugf("Filtering possible duplicate clean targets: %s", color.YellowString("%v", c.absPaths)) |
| 94 | + c.absPaths = removeDuplicatesTargets(c.absPaths) |
| 95 | + prt.Debugf("Processing deduplicated clean targets: %s", color.CyanString("%v", c.absPaths)) |
| 96 | + if execErr := c.execute(); execErr != nil { |
| 97 | + return execErr |
| 98 | + } |
| 99 | + |
| 100 | + // Reject invalid or unsupported JSON data structures. |
| 101 | + default: |
| 102 | + prt.Debugf("unsupported clean configuration type: %s", color.RedString("%T", configType)) |
| 103 | + return fmt.Errorf("unsupported clean configuration") |
| 104 | + } |
| 105 | + |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +func (c *Clean) execute() error { |
| 110 | + for _, targetAbsPath := range c.absPaths { |
| 111 | + // Ignore targets where the directory or file does not exist... |
| 112 | + nodeInfo, nodeInfoErr := os.Lstat(targetAbsPath) |
| 113 | + if os.IsNotExist(nodeInfoErr) { |
| 114 | + prt.Debugf("Ignoring non-existent clean target: %s", color.RedString(targetAbsPath)) |
| 115 | + continue |
| 116 | + // ...and fail if any error occurs while trying to describe the node at the given path. |
| 117 | + } else if nodeInfoErr != nil { |
| 118 | + return nodeInfoErr |
| 119 | + } |
| 120 | + |
| 121 | + t := &target{absPath: targetAbsPath, nodeInfo: nodeInfo, path: targetAbsPath} |
| 122 | + isSymlink, symlinkChkErr := filesystem.IsSymlink(t.absPath) |
| 123 | + if symlinkChkErr != nil { |
| 124 | + return symlinkChkErr |
| 125 | + } |
| 126 | + if isSymlink { |
| 127 | + t.isSymlink = true |
| 128 | + } |
| 129 | + c.targets = append(c.targets, t) |
| 130 | + } |
| 131 | + |
| 132 | + for _, t := range c.targets { |
| 133 | + // Handle the target when it is a symbolic link... |
| 134 | + if t.isSymlink { |
| 135 | + if brokenSymlinkErr := c.handleBrokenSnowblockSymlink(t.absPath); brokenSymlinkErr != nil { |
| 136 | + return brokenSymlinkErr |
| 137 | + } |
| 138 | + continue |
| 139 | + } |
| 140 | + |
| 141 | + // ...or traverse all nodes when it is a directory. |
| 142 | + if t.nodeInfo.IsDir() { |
| 143 | + nodes, nodesListErr := ioutil.ReadDir(t.absPath) |
| 144 | + if nodesListErr != nil { |
| 145 | + return fmt.Errorf("could not read clean target directory content: %s", color.RedString("%v", nodesListErr)) |
| 146 | + } |
| 147 | + for _, targetNode := range nodes { |
| 148 | + nodeAbsPath := filepath.Join(t.absPath, targetNode.Name()) |
| 149 | + isSymlink, symlinkChkErr := filesystem.IsSymlink(nodeAbsPath) |
| 150 | + if symlinkChkErr != nil { |
| 151 | + return symlinkChkErr |
| 152 | + } |
| 153 | + if isSymlink { |
| 154 | + if brokenSymlinkErr := c.handleBrokenSnowblockSymlink(nodeAbsPath); brokenSymlinkErr != nil { |
| 155 | + return brokenSymlinkErr |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return nil |
| 163 | +} |
| 164 | + |
| 165 | +// isSnowblockSymlink checks if the symbolic link at the given absolute path is a broken link of a snowblock node. |
| 166 | +// Returns any error that might occur during the process, nil otherwise. |
| 167 | +func (c *Clean) handleBrokenSnowblockSymlink(absPath string) error { |
| 168 | + // Dissolve the absolute path of the symbolic link and remove it... |
| 169 | + destPath, destPathErr := os.Readlink(absPath) |
| 170 | + if destPathErr != nil { |
| 171 | + return fmt.Errorf("could not read symbolic link: %v", destPathErr) |
| 172 | + } |
| 173 | + if !filepath.IsAbs(destPath) { |
| 174 | + destAbsPath, destAbsPathErr := filepath.Abs(filepath.Join(filepath.Dir(absPath), destPath)) |
| 175 | + if destAbsPathErr != nil { |
| 176 | + return fmt.Errorf("could not dissolve absolute path: %v", destAbsPathErr) |
| 177 | + } |
| 178 | + destPath = destAbsPath |
| 179 | + } |
| 180 | + nodeExists, nodeExistsErr := filesystem.NodeExists(destPath) |
| 181 | + if nodeExistsErr != nil { |
| 182 | + return nodeExistsErr |
| 183 | + } |
| 184 | + // ...when the underlying node does not exist... |
| 185 | + if !nodeExists { |
| 186 | + // ...and the path is a subdirectory of the snowblock directory. |
| 187 | + if strings.HasPrefix(destPath, c.snowblockAbsPath) { |
| 188 | + if removeErr := os.Remove(absPath); removeErr != nil { |
| 189 | + return removeErr |
| 190 | + } |
| 191 | + prt.Infof("Removed broken symbolic link: %s → %s", |
| 192 | + color.YellowString(absPath), color.RedString(destPath)) |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + return nil |
| 197 | +} |
| 198 | + |
| 199 | +// removeDuplicatesTargets removes all duplicate target paths. |
| 200 | +func removeDuplicatesTargets(targets []string) []string { |
| 201 | + encountered := map[string]bool{} |
| 202 | + // Create a map of all unique targets... |
| 203 | + for t := range targets { |
| 204 | + encountered[targets[t]] = true |
| 205 | + } |
| 206 | + var result []string |
| 207 | + // ... and convert all keys from the map into a slice. |
| 208 | + for key := range encountered { |
| 209 | + result = append(result, key) |
| 210 | + } |
| 211 | + |
| 212 | + return result |
| 213 | +} |
0 commit comments