Skip to content

Commit

Permalink
Adds a sprinkle of memes and a watch for files cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr Tihomirov committed Jan 5, 2017
1 parent 8798d7e commit 240f082
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/gowo/cmd/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
// uploadCmd represents the upload command
var uploadCmd = &cobra.Command{
Use: "upload",
Aliases: []string{"up"},
Aliases: []string{"up", "whats"},
Short: "Upload files to OwO",
Run: func(cmd *cobra.Command, args []string) {
cdn := viper.GetString("cdn")
Expand Down
135 changes: 135 additions & 0 deletions cmd/gowo/cmd/watch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// MIT License

// Copyright (c) 2016 @zet4 / @Zeta#2229 / <[email protected]>

// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package cmd

import (
"bytes"
"context"
"fmt"
"log"
"time"

"github.com/atotto/clipboard"
"github.com/spf13/cobra"
"github.com/spf13/viper"
owo "github.com/whats-this/owo.go"

"sync"

"github.com/fsnotify/fsnotify"
)

var queue = make(map[string]struct{})
var queueLock = sync.Mutex{}

// watchCmd represents the upload command
var watchCmd = &cobra.Command{
Use: "watch",
Aliases: []string{"w", "this"},
Short: "Watches directory and uploads new files to OwO",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
log.Fatal("Need at least one location.")
}

cdn := viper.GetString("cdn")

w, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}

for _, arg := range args {
err = w.Add(arg)
if err != nil {
log.Fatal(err)
}
}

go func() {
ticker := time.NewTicker(time.Second)
for range ticker.C {
var names []string
queueLock.Lock()
for name := range queue {
names = append(names, name)
delete(queue, name)
}
queueLock.Unlock()

if len(names) == 0 {
continue
}

files, err := owo.FilesToNamedReaders(names)
if err != nil {
log.Println("[upload]", err)
return
}
response, err := owo.UploadFiles(context.Background(), files)
if err != nil {
log.Println("[upload]", err)
return
}
if !response.Success {
log.Printf("[upload] %d: %s", response.Errorcode, response.Description)
return
}
buf := bytes.Buffer{}
for _, file := range response.Files {
if file.Error {
log.Printf("%d: %s", file.Errorcode, file.Description)
continue
}
fmt.Fprintf(&buf, "%s\n", file.WithCDN(cdn))
}
if shouldClipboard {
err = clipboard.WriteAll(buf.String())
if err != nil {
log.Println("[upload]", err)
return
}
log.Printf("Wrote %d URLs to clipboard", len(response.Files))
} else {
fmt.Print(buf.String())
}
}
}()

log.Print("Started watching for events in given destinations.")
for ev := range w.Events {
if ev.Op&fsnotify.Create == fsnotify.Create {
name := ev.Name
go func() {
time.Sleep(2 * time.Second)
queueLock.Lock()
queue[name] = struct{}{}
queueLock.Unlock()
}()
}
}
},
}

func init() {
uploadCmd.AddCommand(watchCmd)
}

0 comments on commit 240f082

Please sign in to comment.