From 87d9a931a3b45fe7382f3ac3f4e3d947808a03c1 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 27 Oct 2021 02:29:00 +0100 Subject: [PATCH] first commit Signed-off-by: Michael --- .gitignore | 1 + LICENSE | 19 +++++++++++++++++++ README.md | 39 +++++++++++++++++++++++++++++++++++++++ go.mod | 5 +++++ main.go | 41 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 105 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 go.mod create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c14a297 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +go.sum \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e98fac0 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +MIT License Copyright (c) 2021 Michael Lindman + +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 (including the next +paragraph) 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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..2aeb3e9 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# Currency + +Simple golang currency converter using [fawazahmed0](https://github.com/fawazahmed0)'s [currency-api](https://github.com/fawazahmed0/currency-api). + +## Installation + +```sh +go install git.0cd.xyz/michael/currency +``` + +## Usage + +```sh +./currency gbp usd 50 +``` + +## License + +```text +MIT License Copyright (c) 2021 Michael Lindman + +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 (including the next +paragraph) 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. +``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ce280c7 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.0cd.xyz/michael/currency + +go 1.17 + +require git.0cd.xyz/michael/toolbox v0.0.0-20211024232522-b1a517857097 // indirect diff --git a/main.go b/main.go new file mode 100644 index 0000000..4c3978f --- /dev/null +++ b/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "os" + "strconv" + + "git.0cd.xyz/michael/toolbox/req" +) + +const url = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/" + +func main() { + from, to, value := ui() + body, _, err := req.Get(url+from+"/"+to+".json", nil) + if err != nil { + log.Fatal(err) + } + var result map[string]interface{} + if err := json.Unmarshal(body, &result); err != nil { + log.Fatal(err) + } + v, ok := result[to].(float64) + if !ok { + log.Fatal("invalid conversion") + } + fmt.Printf("%.2f %s\n", v*value, to) +} + +func ui() (from, to string, value float64) { + if len(os.Args) < 4 { + os.Exit(1) + } + val, err := strconv.ParseFloat(os.Args[3], 64) + if err != nil { + log.Fatal(err) + } + return os.Args[1], os.Args[2], val +}