8000 Add command for removing libraries or releases by per1234 · Pull Request #73 · arduino/libraries-repository-engine · GitHub
[go: up one dir, main page]

Skip to content

Add command for removing libraries or releases #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# See: https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore

/test/testdata/test_all/golden/logs/
/test/testdata/test_sync/golden/logs/
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ task go:test

Create a `config.json` file (or edit example one). Same thing for `repos.txt` file.

Run with `go run sync_libraries.go` or `task go:build` and then `./libraries-repository-engine`
Run the following command to list the available command line interfaces:

```
./libraries-repository-engine help
```

## Security

Expand Down
107 changes: 107 additions & 0 deletions internal/backup/backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// This file is part of libraries-repository-engine.
//
// Copyright 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.

// Package backup does backup and restore of files.
package backup

import (
"github.com/arduino/go-paths-helper"
)

type backup struct {
originalPath *paths.Path
backupPath *paths.Path
}

var backupsFolder *paths.Path
var backups []backup

// Backup saves a backup copy of the given path.
func Backup(originalPath *paths.Path) error {
if backupsFolder == nil {
// Create a parent folder to store all backups of this session.
var err error
if backupsFolder, err = paths.MkTempDir("", "libraries-repository-engine-backup"); err != nil {
return err
}
}

// Create a folder for this individual backup item.
backupFolder, err := backupsFolder.MkTempDir("")
if err != nil {
return err
}

backupPath := backupFolder.Join(originalPath.Base())

isDir, err := originalPath.IsDirCheck()
if err != nil {
return err
}
if isDir {
if err := originalPath.CopyDirTo(backupPath); err != nil {
return err
}
} else {
if err := originalPath.CopyTo(backupPath); err != nil {
return err
}
}

backups = append(backups, backup{originalPath: originalPath, backupPath: backupPath})

return nil
}

// Restore restores all backed up files.
func Restore() error {
for _, backup := range backups {
isDir, err := backup.backupPath.IsDirCheck()
if err != nil {
return err
}
if isDir {
if err := backup.originalPath.RemoveAll(); err != nil {
return err
}
if err := backup.backupPath.CopyDirTo(backup.originalPath); err != nil {
return err
}
} else {
if err := backup.backupPath.CopyTo(backup.originalPath); err != nil {
return err
}
}
}

return nil
}

// Clean deletes all the backup files.
func Clean() error {
if backupsFolder == nil {
return nil
}

return backupsFolder.RemoveAll()
}
94 changes: 94 additions & 0 deletions internal/backup/backup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// This file is part of libraries-repository-engine.
//
// Copyright 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.

package backup

import (
"testing"

"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var testDataPath string

func TestAll(t *testing.T) {
var err error
originalsFolder, err := paths.MkTempDir("", "backup-test-testall")
require.NoError(t, err)

// Generate test content.
originalContent := []byte("foo")
modifyFile, err := paths.WriteToTempFile(originalContent, originalsFolder, "")
require.NoError(t, err)
modifyFolder, err := originalsFolder.MkTempDir("")
require.NoError(t, err)
modifyFolderFile, err := paths.WriteToTempFile(originalContent, modifyFolder, "")
require.NoError(t, err)
deleteFile, err := paths.WriteToTempFile(originalContent, originalsFolder, "")
require.NoError(t, err)
deleteFolder, err := originalsFolder.MkTempDir("")
require.NoError(t, err)
deleteFolderFile, err := paths.WriteToTempFile(originalContent, deleteFolder, "")
require.NoError(t, err)

// Backup test content.
err = Backup(modifyFile)
require.NoError(t, err)
err = Backup(modifyFolder)
require.NoError(t, err)
err = Backup(deleteFile)
require.NoError(t, err)
err = Backup(deleteFolder)
require.NoError(t, err)

// Change the originals.
err = modifyFile.WriteFile([]byte("bar"))
require.NoError(t, err)
err = modifyFolderFile.WriteFile([]byte("bar"))
require.NoError(t, err)
err = deleteFile.Remove()
require.NoError(t, err)
err = deleteFolder.RemoveAll()
require.NoError(t, err)

err = Restore()
require.NoError(t, err)

// Verify changes to originals were reverted.
content, err := modifyFile.ReadFile()
require.NoError(t, err)
assert.Equal(t, originalContent, content)

content, err = modifyFolderFile.ReadFile()
require.NoError(t, err)
assert.Equal(t, originalContent, content)

assert.True(t, deleteFile.Exist())
assert.True(t, deleteFolderFile.Exist())

// Clean the backups.
err = Clean()
require.NoError(t, err)
}
46 changes: 46 additions & 0 deletions internal/cli/remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This file is part of libraries-repository-engine.
//
// Copyright 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.

package cli

import (
"github.com/arduino/libraries-repository-engine/internal/command/remove"
"github.com/spf13/cobra"
)

// removeCmd defines the `remove` CLI subcommand.
var removeCmd = &cobra.Command{
Short: "Remove libraries or releases",
Long: "Remove libraries or library releases from Library Manager",
DisableFlagsInUseLine: true,
Use: `remove [FLAG]... LIBRARY_NAME[@RELEASE]...

Remove library name LIBRARY_NAME Library Manager content entirely.
-or-
Remove release RELEASE of library name LIBRARY_NAME from the Library Manager content.`,
Run: remove.Run,
}

func init() {
rootCmd.AddCommand(removeCmd)
}
Loading
0