Skip to content

Commit

Permalink
support file as source for URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
reddec committed May 16, 2018
1 parent 47a90f3 commit 061e9ab
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
![vault-sync](https://user-images.githubusercontent.com/6597086/40061189-0d140284-5861-11e8-9e78-1d5d4f8d8e48.png)


* Supports URL files as source

Example: `vault-push -U /path/to/urls1.txt -U /path/to/urls2.txt ...`
10 changes: 9 additions & 1 deletion cmd/vault-drop/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"os"
"github.com/reddec/vault"
"fmt"
"github.com/reddec/vault/utils"
)

var config struct {
URL []string `short:"u" long:"url" env:"URL" description:"storage url"`
URL []string `short:"u" long:"url" env:"URL" description:"storage url"`
URLFiles []string `short:"U" long:"url-file" env:"URL_FILES" description:"file with lines of storage URLs"`
Args struct {
ID string
} `positional-args:"yes" required:"yes"`
Expand Down Expand Up @@ -41,6 +43,12 @@ func main() {
wr.Add(url)
}

err = utils.ReadUrlFiles(&wr, config.URLFiles)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read urls file: %v\n", err)
os.Exit(2)
}

network := vault.SimpleNet(&wr)

err = vault.Delete(network, config.Args.ID)
Expand Down
12 changes: 10 additions & 2 deletions cmd/vault-pull/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"os"
"github.com/reddec/vault"
"fmt"
"github.com/reddec/vault/utils"
)

var config struct {
URL []string `short:"u" long:"url" env:"URL" description:"storage url"`
URL []string `short:"u" long:"url" env:"URL" description:"storage url"`
URLFiles []string `short:"U" long:"url-file" env:"URL_FILES" description:"file with lines of storage URLs"`
Args struct {
ID string
} `positional-args:"yes" required:"yes"`
Expand Down Expand Up @@ -39,12 +41,18 @@ func main() {
wr.Add(url)
}

err = utils.ReadUrlFiles(&wr, config.URLFiles)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read urls file: %v\n", err)
os.Exit(2)
}

network := vault.SimpleNet(&wr)

amount, err := vault.Read(network, config.Args.ID, os.Stdout)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read: %v\n", err)
os.Exit(2)
os.Exit(3)
}
fmt.Fprintln(os.Stderr, "read", amount, "bytes")
}
11 changes: 9 additions & 2 deletions cmd/vault-push/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"os"
"github.com/reddec/vault"
"fmt"
"github.com/reddec/vault/utils"
)

var config struct {
URL []string `short:"u" long:"url" env:"URLS" description:"storage url"`
URLFiles []string `short:"U" long:"url-file" env:"URL_FILES" description:"file with lines of storage URLs"`
ChunkSize int `short:"c" long:"chunk" env:"CHUNK" description:"chunk size in kb" default:"1024"`
Redundancy int `short:"r" long:"redundancy" env:"REDUNDANCY" description:"number of copies" default:"3"`
Args struct {
Expand All @@ -26,7 +28,6 @@ Write stream from stdin to object with specified redundancy and chunk size
`
)


func main() {
parser := flags.NewParser(&config, flags.Default)
parser.ShortDescription = fmt.Sprint(name, " part of VAULT tools - distributes master-less object storage")
Expand All @@ -42,12 +43,18 @@ func main() {
wr.Add(url)
}

err = utils.ReadUrlFiles(&wr, config.URLFiles)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read urls file: %v\n", err)
os.Exit(2)
}

network := vault.SimpleNet(&wr)

amount, err := vault.WriteStream(network, config.Args.ID, config.Redundancy, config.ChunkSize*1024, os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to write: %v\n", err)
os.Exit(2)
os.Exit(3)
}
fmt.Println("written", amount, "bytes")
}
10 changes: 9 additions & 1 deletion cmd/vault-sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"os"
"github.com/reddec/vault"
"fmt"
"github.com/reddec/vault/utils"
)

var config struct {
URL []string `short:"u" long:"url" env:"URLS" description:"storage url"`
URLFiles []string `short:"U" long:"url-file" env:"URL_FILES" description:"file with lines of storage URLs"`
Redundancy int `short:"r" long:"redundancy" env:"REDUNDANCY" description:"number of copies" default:"3"`
}

Expand Down Expand Up @@ -37,12 +39,18 @@ func main() {
wr.Add(url)
}

err = utils.ReadUrlFiles(&wr, config.URLFiles)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read urls file: %v\n", err)
os.Exit(2)
}

network := vault.SimpleNet(&wr)

items, err := network.Sync(config.Redundancy)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to sync: %v\n", err)
os.Exit(2)
os.Exit(3)
}
fmt.Println("fixed", items, "chunks")
}
24 changes: 24 additions & 0 deletions utils/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package utils

import (
"github.com/reddec/vault"
"io/ioutil"
"strings"
)

func ReadUrlFiles(wh *vault.SimpleWarehouse, files []string) error {
for _, file := range files {
content, err := ioutil.ReadFile(file)
if err != nil {
return err
}
for _, line := range strings.Split(string(content), "\n") {
line = strings.TrimSpace(line)
if len(line) == 0 || line[0] == '#' {
continue
}
wh.Add(line)
}
}
return nil
}

0 comments on commit 061e9ab

Please sign in to comment.