Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
thedevsaddam committed Oct 29, 2021
0 parents commit 17b6196
Show file tree
Hide file tree
Showing 28 changed files with 2,052 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
vendor
*.DS_Store
dl
.coverage.txt
linux_386
linux_amd64
mac_amd64
windows_386.exe
windows_amd64.exe
file_example_MP3_700KB.mp3
Apex.2021.720p.WEBRip.x264.AAC-%5BYTS.MX%5D.mp4
go1.14.2.src.tar.gz
12 changes: 12 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Contributing

## Must follow the guide for issues
- Use the search tool before opening a new issue.
- Please provide source code and stack trace if you found a bug.
- Please review the existing issues and provide feedback to them

## Pull Request Process
- Open your pull request against `dev` branch
- It should pass all tests in the available continuous integrations systems such as TravisCI.
- You should add/modify tests to cover your proposed code changes.
- If your pull request contains a new feature, please document it on the README.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# The MIT License (MIT)

Copyright (c) 2021 Saddam H <[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.
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.PHONY: all test coverage
all: test build
build:
go build -o dl
binary:
go run generate-asset.go
./build.sh
install:
go install ./...
test:
go test ./... -v -coverprofile .coverage.txt
go tool cover -func .coverage.txt
coverage: test
go tool cover -html=.coverage.txt
154 changes: 154 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
DL
---
Command-line file downloader tool

### Usage

```sh
$ dl -u https://www.url.com/foo.ext
```

![Alt Text](example.gif)


#### Installation on Mac/Linux
```bash
curl https://raw.githubusercontent.com/thedevsaddam/dl/main/install.sh -o install.sh \
&& sudo chmod +x install.sh \
&& sudo ./install.sh \
&& rm install.sh
```

#### Uninstallation
```bash
curl https://raw.githubusercontent.com/thedevsaddam/dl/main/uninstall.sh -o uninstall.sh \
&& sudo chmod +x uninstall.sh \
&& sudo ./uninstall.sh \
&& rm uninstall.sh
```

#### Windows
**For windows download the binary and set environment variable so that you can access the binary from terminal**

**[Download Binary](https://github.com/thedevsaddam/dl/releases)**

### Download options

```sh
$ dl -u https://www.url.com/foo.ext
# or with concurrency
$ dl -u https://www.url.com/foo.ext -c 10
# with debug mode enabled (if you are curious!)
$ dl -u https://www.url.com/foo.ext -c 10 -d
# with custom output file name
$ dl -u https://www.url.com/foo.ext -c 10 -d -n bar.ext
```
### Configurations

**Setup destination directory**

```sh
# set current directory path as destination directory
$ dl config -p .
# or set other directory
$ dl config -p $HOME/Downloads
```
Note: If you have `destination` directory set then all files automatically will be saved there. If you want to override on the fly then use `-p .` flag to download in the current directory.
By default the `destination` directory value is empty that means the file will be downloaded in the current directory.

**Setup sub-directory/extensions**

Note: If `destination` directory is not set then the sub-directory/extensions will NOT WORK

```sh
# set sub-directory based on extensions
# this will create a "binary" diectory inside root directory.
# all downloads with .exe, .dmg extensions will be stored there
$ dl config -s "binary:.exe,.dmg"
```

**Setup concurrency**

```sh
# default concurrency is 5, you can set default value by passing -c flag in config
$ dl config -c 10
```

### Default configurations

<details><summary>config.json</summary>
<pre>
{
"directory":"",
"concurrency":5,
"sub_dir_map":{
"audio":[
".aif",
".cda",
".mid",
".midi",
".mp3",
".mpa",
".ogg",
".wav",
".wma",
".wpl"
],
"document":[
".xls",
".xlsm",
".xlsx",
".ods",
".doc",
".odt",
".pdf",
".rtf",
".tex",
".txt",
".wpd",
".md"
],
"image":[
".ai",
".bmp",
".ico",
".jpeg",
".jpg",
".png",
".ps",
".psd",
".svg",
".tif",
".tiff"
],
"video":[
".3g2",
".3gp",
".avi",
".flv",
".h264",
".m4v",
".mkv",
".mov",
".mp4",
".mpg",
".mpeg",
".rm",
".swf",
".vob",
".wmv"
]
}
}
</pre>
</details>


### Contribution
Your suggestions will be more than appreciated.
[Read the contribution guide here](CONTRIBUTING.md)

### See all [contributors](https://github.com/thedevsaddam/dl/graphs/contributors)

### **License**
The **dl** is an open-source software licensed under the [MIT License](LICENSE.md).
Binary file added assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash

GIT_COMMIT=$(git rev-parse --short HEAD)
TAG=$(git describe --exact-match --abbrev=0 --tags ${COMMIT} 2> /dev/null || true)
DATE=$(date +'%Y-%m-%d')

echo "Building binaries"
echo Git commit: $GIT_COMMIT Version: $TAG Build date: $DATE

go generate

# MAC
export GOARCH="amd64"
export GOOS="darwin"
export CGO_ENABLED=1
go build -ldflags "-X github.com/thedevsaddam/dl/cmd.GitCommit=$GIT_COMMIT -X github.com/thedevsaddam/dl/cmd.Version=$TAG -X github.com/thedevsaddam/dl/cmd.BuildDate=$DATE" -o mac_amd64 -v .

#LINUX
export GOARCH="amd64"
export GOOS="linux"
export CGO_ENABLED=0
go build -ldflags "-X github.com/thedevsaddam/dl/cmd.GitCommit=$GIT_COMMIT -X github.com/thedevsaddam/dl/cmd.Version=$TAG -X github.com/thedevsaddam/dl/cmd.BuildDate=$DATE" -o linux_amd64 -v

export GOARCH="386"
export GOOS="linux"
export CGO_ENABLED=0
go build -ldflags "-X github.com/thedevsaddam/dl/cmd.GitCommit=$GIT_COMMIT -X github.com/thedevsaddam/dl/cmd.Version=$TAG -X github.com/thedevsaddam/dl/cmd.BuildDate=$DATE" -o linux_386 -v

#WINDOWS
export GOARCH="386"
export GOOS="windows"
export CGO_ENABLED=0
go build -ldflags "-X github.com/thedevsaddam/dl/cmd.GitCommit=$GIT_COMMIT -X github.com/thedevsaddam/dl/cmd.Version=$TAG -X github.com/thedevsaddam/dl/cmd.BuildDate=$DATE" -o windows_386.exe -v

export GOARCH="amd64"
export GOOS="windows"
export CGO_ENABLED=0
go build -ldflags "-X github.com/thedevsaddam/dl/cmd.GitCommit=$GIT_COMMIT -X github.com/thedevsaddam/dl/cmd.Version=$TAG -X github.com/thedevsaddam/dl/cmd.BuildDate=$DATE" -o windows_amd64.exe -v

echo "Build complete"
72 changes: 72 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cmd

import (
"encoding/json"
"fmt"
"log"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/thedevsaddam/dl/config"
)

var (
path string
subPath string

cmdConfig = &cobra.Command{
Use: "config",
Short: "Config set configuration values",
Long: `Config set configuration values`,
Run: setConfig,
}
)

func init() {
cmdConfig.Flags().StringVarP(&path, "path", "p", "", "destination directory where the file will be downloaded")
cmdConfig.Flags().StringVarP(&subPath, "subpath", "s", "", "sub directory map in this format subdirectoryName:.ext1,.ext2. e.g: video:.mp4,.mkv")
cmdConfig.Flags().IntVarP(&concurrent, "concurrent", "c", 0, "number of concurrent process will be running, default: 5")
cmdConfig.Flags().BoolVarP(&debug, "debug", "d", false, "display configuration")
cmdDL.AddCommand(cmdConfig)
}

func setConfig(cmd *cobra.Command, args []string) {
// if user provide "." then set current directory as root directory
if path == "." {
dir, err := os.Getwd()
if err != nil {
log.Fatalln(err)
}
path = dir
}

newCfg := config.Config{Directory: path, Concurrency: uint(concurrent)}

if subPath != "" {
newCfg.SubDirMap = config.DefaultConfig().SubDirMap // assign old config

pp := strings.Split(subPath, ":")
if len(pp) > 1 {
subDir := pp[0]
extensions := strings.Split(pp[1], ",")
for _, e := range extensions {
newCfg.SubDirMap.Add(subDir, e)
}
}
}

if err := config.SetConfig(newCfg); err != nil {
log.Fatalln(err)
}

if debug {
if err := config.LoadDefaultConfig(); err != nil {
log.Fatalln(err)
}
bb, err := json.MarshalIndent(config.DefaultConfig(), "", " ")
if err == nil {
fmt.Println(string(bb))
}
}
}
Loading

0 comments on commit 17b6196

Please sign in to comment.