|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "regexp" |
| 7 | + |
| 8 | + "github.com/drycc/controller-sdk-go/api" |
| 9 | + "github.com/drycc/controller-sdk-go/volumes" |
| 10 | +) |
| 11 | + |
| 12 | +// VolumesList list volumes in the application |
| 13 | +func (d *DryccCmd) VolumesList(appID string, results int) error { |
| 14 | + s, appID, err := load(d.ConfigFile, appID) |
| 15 | + |
| 16 | + if err != nil { |
| 17 | + return err |
| 18 | + } |
| 19 | + |
| 20 | + if results == defaultLimit { |
| 21 | + results = s.Limit |
| 22 | + } |
| 23 | + fmt.Println("-----app-----", appID) |
| 24 | + volumes, count, err := volumes.List(s.Client, appID, results) |
| 25 | + if d.checkAPICompatibility(s.Client, err) != nil { |
| 26 | + return err |
| 27 | + } |
| 28 | + |
| 29 | + if count == 0 { |
| 30 | + d.Println("Could not find any volume") |
| 31 | + } else { |
| 32 | + printVolumes(d, appID, volumes, d.WOut) |
| 33 | + } |
| 34 | + return nil |
| 35 | +} |
| 36 | + |
| 37 | +// printVolumes format volume data |
| 38 | +func printVolumes(d *DryccCmd, appID string, volumes api.Volumes, wOut io.Writer) { |
| 39 | + //volumes := ps.ByType(input) |
| 40 | + |
| 41 | + fmt.Fprintf(wOut, "=== %s Volumes\n", appID) |
| 42 | + |
| 43 | + for _, volume := range volumes { |
| 44 | + fmt.Fprintf(wOut, "--- %s %s\n", volume.Name, volume.Size) |
| 45 | + for k, v := range volume.Path { |
| 46 | + fmt.Println(k, v) |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// VolumesCreate create a volume for the application |
| 52 | +func (d *DryccCmd) VolumesCreate(appID, name string, size string) error { |
| 53 | + s, appID, err := load(d.ConfigFile, appID) |
| 54 | + |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + d.Printf("Creating %s to %s... ", name, appID) |
| 60 | + |
| 61 | + quit := progress(d.WOut) |
| 62 | + volume := api.Volume{ |
| 63 | + Name: name, |
| 64 | + Size: size, |
| 65 | + } |
| 66 | + _, err = volumes.Create(s.Client, appID, volume) |
| 67 | + quit <- true |
| 68 | + <-quit |
| 69 | + if d.checkAPICompatibility(s.Client, err) != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + d.Println("done") |
| 74 | + return nil |
| 75 | +} |
| 76 | + |
| 77 | +// VolumesDelete delete a volume from the application |
| 78 | +func (d *DryccCmd) VolumesDelete(appID, name string) error { |
| 79 | + s, appID, err := load(d.ConfigFile, appID) |
| 80 | + |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + d.Printf("Deleting %s from %s... ", name, appID) |
| 86 | + |
| 87 | + quit := progress(d.WOut) |
| 88 | + err = volumes.Delete(s.Client, appID, name) |
| 89 | + quit <- true |
| 90 | + <-quit |
| 91 | + if d.checkAPICompatibility(s.Client, err) != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + d.Println("done") |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +// VolumesMount mount a volume to process of the application |
| 100 | +func (d *DryccCmd) VolumesMount(appID string, name string, volumeVars []string) error { |
| 101 | + s, appID, err := load(d.ConfigFile, appID) |
| 102 | + |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + volumeMap, err := parseVolume(volumeVars) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + d.Print("Mounting volume... ") |
| 113 | + |
| 114 | + quit := progress(d.WOut) |
| 115 | + volumeObj := api.Volume{Path: volumeMap} |
| 116 | + _, err = volumes.Mount(s.Client, appID, name, volumeObj) |
| 117 | + quit <- true |
| 118 | + <-quit |
| 119 | + if d.checkAPICompatibility(s.Client, err) != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + |
| 123 | + d.Print("done\n\n") |
| 124 | + |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +// VolumesUnmount unmount a volume from process of the application |
| 129 | +func (d *DryccCmd) VolumesUnmount(appID string, name string, volumeVars []string) error { |
| 130 | + s, appID, err := load(d.ConfigFile, appID) |
| 131 | + |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + // volumeMap, err := parseVolume(volumeVars) |
| 137 | + valuesMap := make(map[string]interface{}) |
| 138 | + for _, volumeVar := range volumeVars { |
| 139 | + valuesMap[volumeVar] = nil |
| 140 | + } |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + |
| 145 | + d.Print("Unmounting volume... ") |
| 146 | + |
| 147 | + quit := progress(d.WOut) |
| 148 | + volumeObj := api.Volume{Path: valuesMap} |
| 149 | + _, err = volumes.Mount(s.Client, appID, name, volumeObj) |
| 150 | + quit <- true |
| 151 | + <-quit |
| 152 | + if d.checkAPICompatibility(s.Client, err) != nil { |
| 153 | + return err |
| 154 | + } |
| 155 | + |
| 156 | + d.Print("done\n\n") |
| 157 | + |
| 158 | + return nil |
| 159 | +} |
| 160 | + |
| 161 | +func parseVolume(volumeVars []string) (map[string]interface{}, error) { |
| 162 | + volumeMap := make(map[string]interface{}) |
| 163 | + |
| 164 | + regex := regexp.MustCompile(`^([A-z_]+[A-z0-9_]*)=([\s\S]*)$`) |
| 165 | + for _, volume := range volumeVars { |
| 166 | + if regex.MatchString(volume) { |
| 167 | + captures := regex.FindStringSubmatch(volume) |
| 168 | + volumeMap[captures[1]] = captures[2] |
| 169 | + } else { |
| 170 | + return nil, fmt.Errorf("'%s' does not match the pattern 'key=var', ex: MODE=test", volume) |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return volumeMap, nil |
| 175 | +} |
0 commit comments