Skip to content

Commit

Permalink
Move knoxite-lib into root folder and cmd into cmd/knoxite
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Apr 22, 2020
1 parent 348131e commit ba09e6a
Show file tree
Hide file tree
Showing 57 changed files with 888 additions and 887 deletions.
2 changes: 1 addition & 1 deletion lib/archive.go → archive.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* knoxite
* Copyright (c) 2016-2018, Christian Muehlhaeuser <muesli@gmail.com>
* Copyright (c) 2016-2020, Christian Muehlhaeuser <muesli@gmail.com>
*
* For license see LICENSE
*/
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions cat.go → cmd/knoxite/cat.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* knoxite
* Copyright (c) 2016-2018, Christian Muehlhaeuser <muesli@gmail.com>
* Copyright (c) 2016-2020, Christian Muehlhaeuser <muesli@gmail.com>
*
* For license see LICENSE
*/
Expand All @@ -11,7 +11,7 @@ import (
"fmt"
"os"

"github.com/knoxite/knoxite/lib"
"github.com/knoxite/knoxite"

"github.com/spf13/cobra"
)
Expand Down
6 changes: 3 additions & 3 deletions clone.go → cmd/knoxite/clone.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* knoxite
* Copyright (c) 2016-2017, Christian Muehlhaeuser <muesli@gmail.com>
* Copyright (c) 2016-2020, Christian Muehlhaeuser <muesli@gmail.com>
*
* For license see LICENSE
*/
Expand All @@ -11,10 +11,10 @@ import (
"fmt"
"path/filepath"

"github.com/klauspost/shutdown2"
shutdown "github.com/klauspost/shutdown2"
"github.com/spf13/cobra"

knoxite "github.com/knoxite/knoxite/lib"
"github.com/knoxite/knoxite"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions ls.go → cmd/knoxite/ls.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* knoxite
* Copyright (c) 2016-2018, Christian Muehlhaeuser <muesli@gmail.com>
* Copyright (c) 2016-2020, Christian Muehlhaeuser <muesli@gmail.com>
*
* For license see LICENSE
*/
Expand All @@ -16,7 +16,7 @@ import (
"github.com/muesli/gotable"
"github.com/spf13/cobra"

knoxite "github.com/knoxite/knoxite/lib"
"github.com/knoxite/knoxite"
)

const timeFormat = "2006-01-02 15:04:05"
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions mount.go → cmd/knoxite/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/*
* knoxite
* Copyright (c) 2016-2017, Christian Muehlhaeuser <muesli@gmail.com>
* Copyright (c) 2016-2020, Christian Muehlhaeuser <muesli@gmail.com>
*
* For license see LICENSE
*/
Expand All @@ -23,7 +23,7 @@ import (
"github.com/spf13/cobra"
"golang.org/x/net/context"

knoxite "github.com/knoxite/knoxite/lib"
"github.com/knoxite/knoxite"
)

var (
Expand Down
267 changes: 267 additions & 0 deletions cmd/knoxite/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
/*
* knoxite
* Copyright (c) 2016-2020, Christian Muehlhaeuser <muesli@gmail.com>
*
* For license see LICENSE
*/

package main

import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"
"syscall"

shutdown "github.com/klauspost/shutdown2"
"github.com/muesli/crunchy"
"github.com/muesli/gotable"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"

"github.com/knoxite/knoxite"
)

// Error declarations
var (
ErrPasswordMismatch = errors.New("Passwords did not match")

repoCmd = &cobra.Command{
Use: "repo",
Short: "manage repository",
Long: `The repo command manages repositories`,
RunE: nil,
}
repoInitCmd = &cobra.Command{
Use: "init",
Short: "initialize a new repository",
Long: `The init command initializes a new repository`,
RunE: func(cmd *cobra.Command, args []string) error {
return executeRepoInit()
},
}
repoCatCmd = &cobra.Command{
Use: "cat",
Short: "display repository information as JSON",
Long: `The cat command displays the internal repository information as JSON`,
RunE: func(cmd *cobra.Command, args []string) error {
return executeRepoCat()
},
}
repoInfoCmd = &cobra.Command{
Use: "info",
Short: "display repository information",
Long: `The info command displays the repository status & information`,
RunE: func(cmd *cobra.Command, args []string) error {
return executeRepoInfo()
},
}
repoAddCmd = &cobra.Command{
Use: "add <url>",
Short: "add another storage backend to a repository",
Long: `The add command adds another storage backend to a repository`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("add needs a URL to be added")
}
return executeRepoAdd(args[0])
},
}
repoPackCmd = &cobra.Command{
Use: "pack",
Short: "pack repository and release redundant data",
Long: `The pack command deletes all unused data chunks from storage`,
RunE: func(cmd *cobra.Command, args []string) error {
return executeRepoPack()
},
}
)

func init() {
repoCmd.AddCommand(repoInitCmd)
repoCmd.AddCommand(repoCatCmd)
repoCmd.AddCommand(repoInfoCmd)
repoCmd.AddCommand(repoAddCmd)
repoCmd.AddCommand(repoPackCmd)
RootCmd.AddCommand(repoCmd)
}

func executeRepoInit() error {
// acquire a shutdown lock. we don't want these next calls to be interrupted
lock := shutdown.Lock()
if lock == nil {
return nil
}
defer lock()

r, err := newRepository(globalOpts.Repo, globalOpts.Password)
if err != nil {
return fmt.Errorf("Creating repository at %s failed: %v", globalOpts.Repo, err)
}

fmt.Printf("Created new repository at %s\n", (*r.BackendManager().Backends[0]).Location())
return nil
}

func executeRepoAdd(url string) error {
// acquire a shutdown lock. we don't want these next calls to be interrupted
lock := shutdown.Lock()
if lock == nil {
return nil
}
defer lock()

r, err := openRepository(globalOpts.Repo, globalOpts.Password)
if err != nil {
return err
}

backend, err := knoxite.BackendFromURL(url)
if err != nil {
return err
}
r.BackendManager().AddBackend(&backend)

err = r.Save()
if err != nil {
return err
}
fmt.Printf("Added %s to repository\n", backend.Location())
return nil
}

func executeRepoCat() error {
r, err := openRepository(globalOpts.Repo, globalOpts.Password)
if err != nil {
return err
}

json, err := json.MarshalIndent(r, "", " ")
if err != nil {
return err
}
fmt.Printf("%s\n", json)
return nil
}

func executeRepoPack() error {
r, err := openRepository(globalOpts.Repo, globalOpts.Password)
if err != nil {
return err
}
index, err := knoxite.OpenChunkIndex(&r)
if err != nil {
return err
}

freedSize, err := index.Pack(&r)
if err != nil {
return err
}

err = index.Save(&r)
if err != nil {
return err
}

fmt.Printf("Freed storage space: %s\n", knoxite.SizeToString(freedSize))
return nil
}

func executeRepoInfo() error {
r, err := openRepository(globalOpts.Repo, globalOpts.Password)
if err != nil {
return err
}

tab := gotable.NewTable([]string{"Storage URL", "Available Space"},
[]int64{-48, 15},
"No backends found.")

for _, be := range r.BackendManager().Backends {
space, _ := (*be).AvailableSpace()
tab.AppendRow([]interface{}{
(*be).Location(),
knoxite.SizeToString(space)})
}

_ = tab.Print()
return nil
}

func openRepository(path, password string) (knoxite.Repository, error) {
if password == "" {
var err error
password, err = readPassword("Enter password:")
if err != nil {
return knoxite.Repository{}, err
}
}

return knoxite.OpenRepository(path, password)
}

func newRepository(path, password string) (knoxite.Repository, error) {
if password == "" {
var err error
password, err = readPasswordTwice("Enter a password to encrypt this repository with:", "Confirm password:")
if err != nil {
return knoxite.Repository{}, err
}
}

return knoxite.NewRepository(path, password)
}

func readPassword(prompt string) (string, error) {
var tty io.WriteCloser
tty, err := os.OpenFile("/dev/tty", os.O_WRONLY, 0)
if err != nil {
tty = os.Stdout
} else {
defer tty.Close()
}

fmt.Fprint(tty, prompt+" ")
buf, err := terminal.ReadPassword(int(syscall.Stdin))
fmt.Fprintln(tty)

return string(buf), err
}

func readPasswordTwice(prompt, promptConfirm string) (string, error) {
pw, err := readPassword(prompt)
if err != nil {
return pw, err
}

crunchErr := crunchy.NewValidator().Check(pw)
if crunchErr != nil {
fmt.Printf("Password is considered unsafe: %v\n", crunchErr)
fmt.Printf("Are you sure you want to use this password (y/N)?: ")
var buf string
_, err = fmt.Scan(&buf)
if err != nil {
return pw, err
}

buf = strings.TrimSpace(buf)
buf = strings.ToLower(buf)
if buf != "y" {
return pw, crunchErr
}
}

pwconfirm, err := readPassword(promptConfirm)
if err != nil {
return pw, err
}
if pw != pwconfirm {
return pw, ErrPasswordMismatch
}

return pw, nil
}
4 changes: 2 additions & 2 deletions restore.go → cmd/knoxite/restore.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* knoxite
* Copyright (c) 2016-2017, Christian Muehlhaeuser <muesli@gmail.com>
* Copyright (c) 2016-2020, Christian Muehlhaeuser <muesli@gmail.com>
*
* For license see LICENSE
*/
Expand All @@ -15,7 +15,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"

knoxite "github.com/knoxite/knoxite/lib"
"github.com/knoxite/knoxite"
)

// Error declarations
Expand Down

0 comments on commit ba09e6a

Please sign in to comment.