Skip to content

Commit

Permalink
Patch v0.1 (#36)
Browse files Browse the repository at this point in the history
* ✔️ Mispells fixes

 On branch devel
 Changes to be committed:
	modified:   cmd/init_test.go
	modified:   cmd/list.go
	modified:   cmd/list_test.go
	modified:   cmd/rm_test.go
	modified:   cmd/start_test.go

* Improvement of the ``rm`` command (#21) (#25)

* ✔️ Mispells fixes

 On branch devel
 Changes to be committed:
	modified:   cmd/init_test.go
	modified:   cmd/list.go
	modified:   cmd/list_test.go
	modified:   cmd/rm_test.go
	modified:   cmd/start_test.go

* 🚧 Work on rework of rm command starts -- bad

 Issues related:
 	-> #21 Improve ``rm`` command

 On branch dev-enhancement-rm
 Changes to be committed:
	modified:   cmd/rm.go

* 💊 Fixed missspelling

 On branch dev-enhancement-rm
 Changes to be committed:
	modified:   cmd/init_test.go
	modified:   cmd/list.go
	modified:   cmd/rm.go
	modified:   cmd/rm_test.go

* ✔️🏁 processArgsRm added + tests

 Issues related:
 	-> #21
 	-> #16
 	-> #9

 On branch dev-enhancement-rm
 Changes to be committed:
	modified:   cmd/init_test.go
	modified:   cmd/rm.go
	modified:   cmd/rm_test.go

* ✔️🏁 New feature for rm
	-> Added & adapted tests only for the ones impacted by those changes

 Issues related:
 	-> #21
 	-> #16

 On branch dev-enhancement-rm
 Changes to be committed:
	modified:   cmd/rm.go
	modified:   cmd/rm_test.go
	modified:   cmd/root.go

* 💻🏁 Added test for rm.go:simpleDelAllString()
	-> Few changes (variadic params instead of slice)
	-> Some update for the ``$HOME`` on Windows

 Issues related:
 	-> #21
 	-> #16
 	-> #9

 On branch dev-enhancement-rm
 Changes to be committed:
	modified:   cmd/purge.go
	modified:   cmd/rm.go
	modified:   cmd/rm_test.go
	modified:   cmd/root.go

* ✔️🏁 Added handler for ``*`` wildcard as argument
	-> Tests, were adapted for that

 Issues related:
 	-> #21

 On branch dev-enhancement-rm
 Changes to be committed:
	modified:   cmd/rm.go
	modified:   cmd/rm_test.go

* 📝 Update of usage for ``rm`` and deletion of deprecated stuff

 Issues related:
 	-> #21
 	-> #19

 On branch dev-enhancement-rm
 Changes to be committed:
	modified:   cmd/rm.go

* Implementation of ``tempest list --fix`` flag (#27)

* 🚧 Added the functionality, now needs the tests

 Issues related:
 	-> #20

* 🏁 Tests for list.go (``--fix`` flag related)
	+ Begin of migration to the use of Target instead of simple strings

 Issues related:
 	-> #20

-----------------------
Merge Fixes #20

* 🔨 Auto mode for TEMPest (#28)

* 🚧 Moved configs files for TEMpest into a directory
	[+] Added .sh for auto-mode
	--> Issues related: #24, #15
* Handler for shutup mode made, need merge
* Stash pop
* 🏁 Added tests for shutup mode handler (start.go)
	-> Issues related: #15, #16
* ✔️🏁 Added logs flag for get command
	[+] Added the corresponding test
	--> Issues related: #15, #16
* ✔️ Added handler for relative paths with ``add``
* 🏁 Added tests for treatRelativePath()
* 🏁 Test for getAllLogs() (--logs)
	--> Issues related: #15, #16
	[*] Should be the end of the Linux part for this one
* 💻 Windows autostart kinda done for theoric part
	[#] Issues related: #15, #9
	[T] Unit tests for set.go:setAutoStart()
	[T] Runtime testing for Windows
* 📛 Hot fix for codecov chmod
* 🏁 Test only for error checks
	[I] #15, #16
	[T] More elaborated tests for setAutoStart()
	[#] Fixes #15

* ➕ New flag for get (auto-mode)
	[+] Also implemented test func for it
	[#] Issues: #15, #16

* 📝 Update of README.md

* ⬆️ dep ensure

* Manage temp files (#30)

* 🚧 Started to refractor purge to purge files
	[T] Keep refratoring each functions affected
	[#] Issues: #17
	[N] Some comments were deleted (cobra related)

* New func to display size + units

* 📄🚽 Implemented the file purging
	[T] Tests
	[#] #30, #17

* 🏁 Tests for purge
👉 Apply TreatLastChar() to conf.Gobin & conf.Gopath

* Targets automation (#34)

* ✔️ Func to find dirs from exact pattern added

* ✔️ Flag on ``add`` command added
🏁 Test for the research func
	[T]	Test the other funcs created

* ✔️ Better error handling for that
👉 TODO? More interaction for this command/flag

* ✔️ command ``new`` implemented to generated new target dirs
🏁 Tests passed

*  Added fallback to cmd.Help() by default

* ✔️ add

* ✔️ init

* ✔️ new
✅ fix init

* ✔️ purge

* ✔️ rm

* ✔️ root

* ✔️ set

* ✔️ start
👉 the rest can wait :(

* 👍 Small update to README.md

* messing

* Update documentation (#37)

*  Added fallback to cmd.Help() by default

* ✔️ add

* ✔️ init

* ✔️ new
✅ fix init

* ✔️ purge

* ✔️ rm

* ✔️ root

* ✔️ set

* ✔️ start
👉 the rest can wait :(

* 👍 Small update to README.md

* messing

* small fix
  • Loading branch information
ChacaS0 committed Jun 8, 2018
1 parent 19ca504 commit 76f2192
Show file tree
Hide file tree
Showing 12 changed files with 464 additions and 48 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ First, to initialize it the first time, run:
```

This will generate a ``.tempestcf`` file in ``$HOME/.tempest``.
It will hold the list of all the temp directories.

It will hold the list of all the targets.

> If there is an issue and the file can't be created somehow, you can still create it at its default location: ``$HOME/.tempest/.tempestcf`` and leave it empty for now.
Expand All @@ -89,34 +90,34 @@ auto-mode: false
```
#### Parameters:
* **duration** : This is the maximum age the content of the temp directories, choose it carefully!
* **duration** : This is the maximum age the content of the target's content, choose it carefully!
> *You have to choose a duration greater than 1 !!*
## Add a new temp folder to the list
## Add a new target to the list
### To add the current directory
Positionate yourself to the deried directory.
For example, if you want to add /tmp, use this instructions:
For example, if you want to add ``/tmp``, use this instructions:
```bash
$ cd /temp
$ tempest add
```

### Through command-line
### With command-line
Just run:

```bash
tempest add ...<PATH>
```

* **``<PATH>``** being the path to the directory to be added to the list of temp directories
* **``<PATH>``** being the path to the directory to be added to the list of targets
* **``...``** meaning that many arguments can be passed

> **By convention we will give the name ``temp`` to the directories to be added to ``TEMPest``**
> **By convention we will give the name ``temp.est`` to the directories to be added to ``TEMPest``**
### Through text editor
Just open ``$HOME/.tempest/.tempestcf`` and add a new line with the <u>absolute path</u> of the temp directory to be added.
### With a text editor
Just open ``$HOME/.tempest/.tempestcf`` and add a new line with the <u>absolute path</u> of the target to be added.

## List the current directories added to TEMPest
### Using TEMPest
Expand Down Expand Up @@ -191,7 +192,6 @@ If you want to know more about **TEMPest**, visit <a href="https://chacas0.githu


-------------------
*Thanks to*
> <a href="https://github.com/golang/go" target="_blank"><img src="https://upload.wikimedia.org/wikipedia/commons/2/23/Golang.png" width=33%></a>
> <a href="https://github.com/spf13/cobra" target="_blank"><img src="https://cloud.githubusercontent.com/assets/173412/10886352/ad566232-814f-11e5-9cd0-aa101788c117.png" width=33%></a>
Expand Down
84 changes: 78 additions & 6 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strings"

Expand All @@ -38,6 +39,10 @@ import (

var this string

// autoAdd indicates if TEMPest should look for all ``temp.est`` dirs and
// add them as targets
var autoAdd bool

// var conf struct {
// Home string
// }
Expand All @@ -55,13 +60,37 @@ tempest add
To add another directory:
tempest add /tmp
> By convention we will name the temporary directories "temp".
> By convention we will name the temporary directories "temp.est".
This way they will be easy to spot
`,
Run: func(cmd *cobra.Command, args []string) {
// fmt.Println(color.HiBlueString("lol"))
if errAddLine := addLine(args); errAddLine != nil {
fmt.Println("::An error occurred while adding path(s):\n", errAddLine)
// Auto add flag used
if autoAdd && len(args) == 0 {
toAdd, errDirs := findDirs("/home/chacanterg/", "temp.est")
if errDirs != nil {
log.Fatal(errDirs)
}
toAdd, errStrip := stripExistingTargets(toAdd)
lenAdd := len(toAdd)
if lenAdd > 0 {
if errStrip != nil {
fmt.Println(errStrip.Error())
return
}
} else {
fmt.Println(cyanB("[INFO]::"), color.HiCyanString("No targets were added"))
}
// add these lines
if errAddLine := addLine(toAdd); errAddLine != nil {
fmt.Println("::An error occurred while adding target(s):\n", errAddLine)
}
} else if !autoAdd {
// FALLBACK CASE
if errAddLine := addLine(args); errAddLine != nil {
fmt.Println("::An error occurred while adding target(s):\n", errAddLine)
}
} else {
cmd.Help()
}

},
Expand All @@ -77,6 +106,7 @@ func init() {
// addCmd.PersistentFlags().String("foo", "", "A help for foo")
// addCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
// addCmd.Flags().StringVarP(&this, "this", "t", "nothing", "Points to current directory")
addCmd.Flags().BoolVarP(&autoAdd, "auto", "a", false, "Look for all ``temp.est`` directories of the system and add them to the targets list of TEMPest if they are not already there.")
}

// addLine add each string as target into TEMPest (~/.tempestcf)
Expand Down Expand Up @@ -123,7 +153,7 @@ func addLine(args []string) error {
fmt.Println(redB("::"), color.RedString("Could not write to the file. Fail bitch!"))
return errWrite
}
fmt.Println(greenB("[NEW TEMP]::")+color.GreenString(fmt.Sprintf("%d", nbBytes)+"::>"), this)
fmt.Println(greenB(":: [SUCCESS] ")+color.GreenString(fmt.Sprintf("NEW TARGET::%d", nbBytes)+"::>\t"), this)
} else {
// Treat the last character
for ind, onePath := range args {
Expand All @@ -147,7 +177,8 @@ func addLine(args []string) error {
if errWS != nil {
fmt.Println(color.RedString(":: Are you sure you can handle this much? Without askin your mom first!?"))
}
fmt.Println(color.GreenString("[NEW TEMP]::"+fmt.Sprintf("%d", nbBytes)+"::>"), path)
// fmt.Println(color.GreenString("[NEW TEMP]::"+fmt.Sprintf("%d", nbBytes)+"::>"), path)
fmt.Println(greenB(":: [SUCCESS] ")+color.GreenString(fmt.Sprintf("NEW TARGET::%d", nbBytes)+"::>\t"), path)
}
fmt.Println(color.YellowString("::"), "All paths were added to TEMPest !")
}
Expand Down Expand Up @@ -238,3 +269,44 @@ func TreatLastChar(str string) string {
}
return str
}

// findDirs returns all paths of the directories matching the pattern from
// the root path provided
func findDirs(root, pattern string) ([]string, error) {
dirs := make([]string, 0)

err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if info.IsDir() && info.Name() == pattern {
dirs = append(dirs, path)
}
return nil
})

return dirs, err
}

// stripExistingTargets takes the slice of targets to add as parameter
// returns a new slice of targets to register but without the existing ones
func stripExistingTargets(wantAdd []string) ([]string, error) {
strippedList := make([]string, 0)
existingTgts, errAllTgt := getPaths()

if errAllTgt != nil {
if errAllTgt.Error() == "empty" {
for _, tgt := range wantAdd {
strippedList = append(strippedList, tgt)
}
return strippedList, nil
}
fmt.Println(redB("[ERROR]::"), color.HiRedString("Failed to fetch existing targets. Check if the config is right\n\t"), errAllTgt)
return nil, errAllTgt
}

for _, tgt := range wantAdd {
if !IsStringInSlice(tgt, existingTgts) {
strippedList = append(strippedList, tgt)
}
}

return strippedList, nil
}
54 changes: 54 additions & 0 deletions cmd/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,57 @@ func TestAddLine(t *testing.T) {
// Clean up the mess
fbTestTempestcf(t, tempestcfbup)
}

// TestFindDirs
func TestFindDirs(t *testing.T) {
// init vars
rootPath := conf.Gopath + Slash + "tests_temp"
testDirs := []string{
0: conf.Gopath + Slash + "tests_temp" + Slash + "dir1",
1: conf.Gopath + Slash + "tests_temp" + Slash + "test",
2: conf.Gopath + Slash + "tests_temp" + Slash + "dir3" + Slash + "test",
3: conf.Gopath + Slash + "tests_temp" + Slash + "dir4" + Slash + "notTest",
}
// setup test envi
if err := createTestDir(rootPath); err != nil {
t.Log("[ERROR]:: Could not create the test dir\n\t->", err)
t.FailNow()
}

param2 := [][]string{
0: testDirs,
}

if err := addToTestDir(rootPath, param2); err != nil {
t.Log("[ERROR]:: Could not create the test content\n\t->", err)
rmTestDirs(rootPath)
t.FailNow()
}

// mock args
args := []struct {
root string
pattern string
want []string
}{
{rootPath, "test", []string{
testDirs[1],
testDirs[2],
}},
}

for _, tst := range args {
slFound, errFound := findDirs(tst.root, tst.pattern)
if errFound != nil {
t.Log("[ERROR]:: Searching for matching directories resulted to an error:\n\t-> ", errFound)
t.Fail()
}
if !SameSliceValuesStr(tst.want, slFound) {
t.Log("[FAILED]:: Wanted:\n\t", tst.want, "\nGot:\n\t", slFound)
t.Fail()
}
}

rmTestDirs(rootPath)

}
16 changes: 11 additions & 5 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import (
// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "Call this if it is the first time you use TEMPest or to reset the temps",
Long: `Call this if it is the first time you use TEMPest or to reset the temps:
Short: "Call this if it is the first time you use TEMPest or to reset the config",
Long: `Call this if it is the first time you use TEMPest or to reset the config:
It will create a file named .tempestcf in your /home/$USER.
This file will contain the list of all the directories you wish tempest to handle as temporary directory.
Expand All @@ -50,7 +50,7 @@ This file will contain the list of all the directories you wish tempest to handl
if err := initializeCfFile(); err != nil {
fmt.Println(redB("::"), color.HiRedString("Could not initialize .tempest.yaml"))
fmt.Println(redB("::"), color.HiRedString("If the error persists, try to create the file manually : touch $HOME/.tempest/.tempest.yaml"))
// fmt.Println(err) //DEBUG

return
}

Expand All @@ -67,7 +67,13 @@ This file will contain the list of all the directories you wish tempest to handl
fmt.Println(greenB("::"), color.HiGreenString("Suggestions:"))
fmt.Println(color.HiGreenString(` Start using TEMPest right away by adding a temporary file :
tempest add <DIRECTORY_PATH>
Or get help to add new paths:
Or generate new targets
tempest new -t -a # Generate a new target in the current directory.
This new target will be already registered.
The created directory will be called "temp.est".
Or find existing potential targets and add them (meaning the ones called "temp.est")
tempest add --auto
Or get help to add new targets:
tempest help add
`))
},
Expand Down Expand Up @@ -96,7 +102,7 @@ func initTempestDir() error {
}
// fmt.Println("conf dir created")
// create the log dir
err := os.Mkdir(TempestConfigDir+string(os.PathSeparator)+".log", 0755)
err := os.Mkdir(TempestConfigDir+Slash+".log", 0755)
// fmt.Println("log dir created if nil =>", err)
return err
}
Expand Down
63 changes: 60 additions & 3 deletions cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,11 @@ func setTestTempestcf(t *testing.T, slTest []string) (tempestcfbup string) {
defer tmpcf.Close()

// Add slTest data to Tempestcf
if err := addLine(slTest); err != nil {
t.Log("[ERROR]:: Can't add lines to", Tempestcf, ":\n\t->", err)
t.Fail()
if len(slTest) > 0 {
if err := addLine(slTest); err != nil {
t.Log("[ERROR]:: Can't add lines to", Tempestcf, ":\n\t->", err)
t.Fail()
}
}

return
Expand Down Expand Up @@ -199,3 +201,58 @@ func fbTestTempestcf(t *testing.T, tempestcfbup string) {
}
Tempestcf = tempestcfbup
}

// createTestDir createa directory meant for testing
func createTestDir(path string) error {
return os.MkdirAll(path, 0777)
}

// addToTestDir add items to the path (test dir)
// ex:
// ```go
// {
// 0: {
// "dir1",
// "dir2",
// },
// 1: {
// "file1",
// "file2",
// }
// }
// ```
func addToTestDir(pathTestDir string, toAdd [][]string) error {
const DIR = 0
const FILE = 1

// Create dirs
if len(toAdd[DIR]) > 0 {
for _, path := range toAdd[DIR] {
if err := os.MkdirAll(path, 0777); err != nil {
return err
}
}
}

if len(toAdd) >= 2 {
for _, path := range toAdd[FILE] {
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
f.Close()
}
}

return nil
}

// rmTestDirs removes the test directory(ies) and everything in it
func rmTestDirs(dirs ...string) error {
for _, pathDir := range dirs {
if err := os.RemoveAll(pathDir); err != nil {
return err
}
}
return nil
}
Loading

0 comments on commit 76f2192

Please sign in to comment.