Code Monkey home page Code Monkey logo

dashf's Introduction

DashF

Build Status

Unmarshall A file, STDIN or a URL into a golang struct. The supported formats are JSON and YAML.

What is DashF?

DashF means the flag -f. When a CLI wishes to accept a resource (filename/directory/url) they typically use the -f flag. DashF provides your cli a consistent way to unmarshall user-defined resources into golang structs. Most CLI tools process the -f flag in the same manner, but the code is typically buried and not easily re-usable.

Doesn't Viper/envconfig/pflag do this?

No. Viper and envconfig provide a great libraries for configuring your application. Viper and pflag can accept the input but provide no mechanism for processing the resource.

Here is some example input JSON. This can be located in a local file, a URL or even piped via STDIN.

{
  "one": 1,
  "two": "two",
  "three": [
    {
      "a": "a",
      "b": "b",
      "c": "c"
    }
  ]
}

Example code to handle the input.

package main

import (
        "fmt"
        "github.com/cdornsife/dashf"
        "github.com/spf13/pflag"
        "os"
)

// TestStruct represents the data in the json/yaml file
// Example files are in the tests directory
type TestStruct struct {
        One   int    `json:"one"`
        Two   string `json:"two"`
        Three []struct {
                A string `json:"a"`
                B string `json:"b"`
                C string `json:"c"`
        } `json:"three"`
}

func main() {

        var inputFile string
        var out = new(TestStruct)

        pflag.StringVarP(&inputFile, "file", "f", "", "Input for create/update: json|yaml|-")
        pflag.Parse()

        err := dashf.Unmarshal(inputFile, &out)
        if err != nil {
                fmt.Printf("%+v\n", err)
                os.Exit(1)
        }

        fmt.Printf("%+v\n", out)
}

Command line usage:

$ cd example

# Pass in a - to use STDIN
$ cat ../tests/test.json | go run main.go -f -

# Specify a local file
$ go run main.go -f ../tests/test.yaml

# Specify a URL
go run main.go --file https://raw.githubusercontent.com/cdornsife/dashf/master/tests/test.yaml

dashf's People

Contributors

cdornsife avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.