Skip to content

Commit

Permalink
cmd: add command line arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
yuqingc committed Oct 1, 2018
1 parent cfe8f18 commit ce0a471
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 25 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,42 @@ Table of Contents
* [rmdashrf (WIP)](#rmdashrf-wip)
* [Table of Contents](#table-of-contents)
* [Get Started](#get-started)
* [Start by running binaries](#start-by-running-binaries)
* [Run a Docker image](#run-a-docker-image)
* [Build from source](#build-from-source)
* [API Documentation](#api-documentation)
* [Roadmap](#roadmap)
* [Contributing](#contributing)
* [Tools](#tools)
* [Dependencies](#dependencies)
* [Debug](#debug)

<!-- Added by: matt, at: 2018-09-24T01:03+08:00 -->
<!-- Added by: matt, at: -->

<!--te-->

## Get Started

### Start by running binaries

1. Download binaries in the [release page]()

2. Start

```
TODO
$ ./rmdashrf -port=8080 -volume=/data
```

The `-port` flag is optional, `8080` by default. Use `./rmdashrf -h` or `./rmdashrf -help` for more information

### Run a Docker image

TODO

### Build from source

TODO

## API Documentation

- [apis](https://github.com/yuqingc/rmdashrf/blob/master/docs/apis.md)
Expand Down
20 changes: 17 additions & 3 deletions cmd/rmdashrf/rfdashrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@ package main

import (
"fmt"
"os"
"strings"

"github.com/yuqingc/rmdashrf/pkg/routes"
"github.com/yuqingc/rmdashrf/pkg/v1handlers"
)

func main() {
const port = "8080"
fmt.Println("RMDASHRF is running at :" + port)
if v1handlers.RequestVersion {
fmt.Println(VERSION)
os.Exit(0)
}
if v1handlers.MountedVolume == "" {
fmt.Println("volume is not specified")
os.Exit(1)
}
if !strings.HasPrefix(v1handlers.MountedVolume, "/") {
fmt.Println("volume must be an absolute path")
os.Exit(1)
}
fmt.Println("RMDASHRF is running at :" + v1handlers.Port)
router := routes.Router
router.Run(":" + port)
router.Run(":" + v1handlers.Port)
}
Binary file added cmd/rmdashrf/rmdashrf
Binary file not shown.
4 changes: 4 additions & 0 deletions cmd/rmdashrf/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package main

// VERSION is the current version of RMDASHRF
const VERSION = "v1.0.0-rc.1"
3 changes: 0 additions & 3 deletions pkg/routes/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ Package routes defines all routes and APIs
package routes

import (
"log"

"github.com/gin-gonic/gin"
"github.com/yuqingc/rmdashrf/pkg/v1handlers"
)
Expand All @@ -14,7 +12,6 @@ import (
var Router = gin.Default()

func init() {
log.Println("loading routes...")
v1 := Router.Group("/api/v1")
{
v1.GET("/default/*contentPath", v1handlers.HandleGet)
Expand Down
5 changes: 0 additions & 5 deletions pkg/v1handlers/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,5 @@ package v1handlers
// BadRequestErrMsg is for general 400 status code
const BadRequestErrMsg = "invalid or too less query params"

// MountDir is hard coded for now. It will be configurable
// TODO: checks if mount dir exists. Create it if not
// TODO: mountpath must be an absolute path
const MountDir = "/home/matt/Projects/github.com/yuqingc/data"

// MaxListResults is the max for listing contents for a specified directory
const MaxListResults = 5000
2 changes: 1 addition & 1 deletion pkg/v1handlers/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func HandleDelete(c *gin.Context) {
return
}

fullPath := path.Join(MountDir, contentPath)
fullPath := path.Join(MountedVolume, contentPath)
paramRecursive := c.Query("recursive")
recursive := false
if paramRecursive == "true" {
Expand Down
4 changes: 2 additions & 2 deletions pkg/v1handlers/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func DownloadFile(c *gin.Context) {
return
}

fullFilePath := path.Join(MountDir, contentPath)
fullFilePath := path.Join(MountedVolume, contentPath)
fileInfo, err := os.Stat(fullFilePath)
if err != nil {
c.Status(http.StatusNotFound)
Expand All @@ -45,7 +45,7 @@ func DownloadDirAsZip(c *gin.Context) {
c.String(http.StatusBadRequest, err.Error())
return
}
fullDirPath := path.Join(MountDir, contentPath)
fullDirPath := path.Join(MountedVolume, contentPath)
dirInfo, err := os.Stat(fullDirPath)
if err != nil {
c.Status(http.StatusNotFound)
Expand Down
26 changes: 26 additions & 0 deletions pkg/v1handlers/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package v1handlers

import (
"flag"
)

// MountedVolume is the volume mounted to this program
var MountedVolume string

// Port is the TCP port for the service
var Port string

// DefaultPort is the default TCP port.
// It can be overwritten by the command line
const DefaultPort = "8080"

// RequestVersion tells if a version flag is provided
var RequestVersion bool

func init() {
flag.StringVar(&MountedVolume, "volume", "", "You must specify a path of which the directory is mounted. It should be an absolute path which starts with slash (\"/\")")
flag.StringVar(&Port, "port", DefaultPort, "The TCP port of RMDASHRF.")
flag.BoolVar(&RequestVersion, "version", false, "Print the current version.")

flag.Parse()
}
2 changes: 1 addition & 1 deletion pkg/v1handlers/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func GetList(c *gin.Context) {
paramMaxresults := c.Query("maxresults")
paramExtension := c.Query("extension")

dir := path.Join(MountDir, contentPath)
dir := path.Join(MountedVolume, contentPath)
all := paramAll == "true"
var maxresults = MaxListResults
if paramMaxresults != "" {
Expand Down
4 changes: 2 additions & 2 deletions pkg/v1handlers/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func Rename(c *gin.Context) {
return
}

oldPath := path.Join(MountDir, paramContentPath)
newPath := path.Join(MountDir, paramTo)
oldPath := path.Join(MountedVolume, paramContentPath)
newPath := path.Join(MountedVolume, paramTo)
if err := manager.Rename(oldPath, newPath); err != nil {
log.Println("rename failed:", err)
var ErrMsg = err.Error()
Expand Down
12 changes: 6 additions & 6 deletions pkg/v1handlers/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func CreateFile(c *gin.Context) {
return
}

fullFilePath := path.Join(MountDir, paramContentPath)
fullFilePath := path.Join(MountedVolume, paramContentPath)
createdFile, err := manager.CreateFile(fullFilePath)
if err != nil {
log.Println(err)
Expand Down Expand Up @@ -77,7 +77,7 @@ func CreateDir(c *gin.Context) {
parents = true
}

fullDirPath := path.Join(MountDir, paramContentPath)
fullDirPath := path.Join(MountedVolume, paramContentPath)
if err := manager.CreateDir(fullDirPath, parents); err != nil {
log.Println(err)
var errMsg = "file or directory already exits"
Expand Down Expand Up @@ -106,8 +106,8 @@ func CopyFile(c *gin.Context) {
return
}

fullDstPath := path.Join(MountDir, paramContentPath)
fullSrcPath := path.Join(MountDir, paramFrom)
fullDstPath := path.Join(MountedVolume, paramContentPath)
fullSrcPath := path.Join(MountedVolume, paramFrom)

if err := manager.CopyFile(fullSrcPath, fullDstPath); err != nil {
log.Println(err)
Expand All @@ -133,8 +133,8 @@ func CopyDir(c *gin.Context) {
return
}

fullDesPath := path.Join(MountDir, paramContentPath)
fullSrcPath := path.Join(MountDir, paramFrom)
fullDesPath := path.Join(MountedVolume, paramContentPath)
fullSrcPath := path.Join(MountedVolume, paramFrom)

if err := manager.CopyDir(fullSrcPath, fullDesPath); err != nil {
log.Println(err)
Expand Down

0 comments on commit ce0a471

Please sign in to comment.