This repository has been archived by the owner on Mar 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean.go
66 lines (61 loc) · 2.39 KB
/
clean.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright © 2017 Sascha Andres <sascha.andres@outlook.com>
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
helper2 "github.com/sascha-andres/devenv/internal/helper"
"log"
"os"
"path"
"strings"
"github.com/sascha-andres/devenv"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// cleanCmd represents the clean command
var cleanCmd = &cobra.Command{
Use: "clean",
Short: "Clean project environment",
Long: `Removes all code from your harddisk. Scans all repositories for uncommitted
changes before deleting the complete directory tree.`,
Run: func(cmd *cobra.Command, args []string) {
projectName := strings.Join(args, " ")
if projectName != "" && devenv.ProjectIsCreated(projectName) {
if viper.GetBool("clean.force") {
log.Print("Forced removal, all changes are lost")
os.RemoveAll(path.Join(viper.GetString("basepath"), projectName))
return
}
var ev devenv.EnvironmentConfiguration
if ok, err := helper2.Exists(path.Join(viper.GetString("configpath"), projectName+".yaml")); ok && err == nil {
if err := ev.LoadFromFile(path.Join(viper.GetString("configpath"), projectName+".yaml")); err != nil {
log.Fatalf("Error reading env config: '%s'", err.Error())
}
}
for _, repo := range ev.Repositories {
if hasChanges, err := helper2.HasChanges(ev.ProcessConfiguration.Environment, path.Join(path.Join(viper.GetString("basepath"), projectName, repo.Path))); hasChanges || err != nil {
log.Printf("'%s' has changes", repo.Name)
os.Exit(1)
}
}
log.Printf("'%s' has no changes, removing", projectName)
os.RemoveAll(path.Join(viper.GetString("basepath"), projectName))
} else {
log.Println("Project does not exist")
}
},
}
func init() {
cleanCmd.PersistentFlags().BoolP("force", "f", false, "Force removal")
viper.BindPFlag("clean.force", cleanCmd.PersistentFlags().Lookup("force"))
RootCmd.AddCommand(cleanCmd)
}