├── .gitignore ├── go.mod ├── go.sum ├── .github └── workflows │ └── go.yml ├── LICENSE ├── curr.go ├── all_test.go └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | coverage.txt 2 | vendor 3 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/otiai10/curr 2 | 3 | go 1.12 4 | 5 | require github.com/otiai10/mint v1.3.0 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE= 2 | github.com/otiai10/mint v1.3.0 h1:Ady6MKVezQwHBkGzLFbrsywyp09Ah7rkmfjV3Bcr5uc= 3 | github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= 4 | -------------------------------------------------------------------------------- /.github/workflows/go.yml: -------------------------------------------------------------------------------- 1 | name: Go 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | 9 | jobs: 10 | 11 | build: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v2 15 | 16 | - name: Set up Go 17 | uses: actions/setup-go@v2 18 | with: 19 | go-version: 1.15 20 | 21 | - name: Build 22 | run: go build -v ./... 23 | 24 | - name: Test 25 | run: go test -v -cover -coverprofile=coverage.txt ./... 26 | 27 | - name: Upload Coverage 28 | uses: codecov/codecov-action@v1 29 | with: 30 | file: coverage.txt 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2020 Hiromu Ochiai 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /curr.go: -------------------------------------------------------------------------------- 1 | package curr 2 | 3 | import ( 4 | "path" 5 | "runtime" 6 | "strings" 7 | ) 8 | 9 | const ( 10 | depthOfFunctionCaller = 1 11 | ) 12 | 13 | // File is current file name provider, 14 | // like `__FILE__` of PHP. 15 | func File() string { 16 | _, fi, _, _ := runtime.Caller(depthOfFunctionCaller) 17 | return fi 18 | } 19 | 20 | // Basename is current file basename provider, 21 | // like `basename(__FILE__)` of PHP. 22 | func Basename() string { 23 | _, fi, _, _ := runtime.Caller(depthOfFunctionCaller) 24 | return path.Base(fi) 25 | } 26 | 27 | // Dir is current directory provider, 28 | // like `__DIR__` of PHP. 29 | func Dir() string { 30 | _, fi, _, _ := runtime.Caller(depthOfFunctionCaller) 31 | return path.Dir(fi) 32 | } 33 | 34 | // Func is current function name provider, 35 | // like `__FUNCTION__` of PHP. 36 | func Func() string { 37 | pc, _, _, _ := runtime.Caller(depthOfFunctionCaller) 38 | fn := runtime.FuncForPC(pc) 39 | elems := strings.Split(fn.Name(), ".") 40 | return elems[len(elems)-1] 41 | } 42 | 43 | // Line is current line provider, 44 | // like `__LINE__` of PHP. 45 | func Line() int { 46 | _, _, li, _ := runtime.Caller(depthOfFunctionCaller) 47 | return li 48 | } 49 | -------------------------------------------------------------------------------- /all_test.go: -------------------------------------------------------------------------------- 1 | package curr 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | . "github.com/otiai10/mint" 8 | ) 9 | 10 | var pkgpath string 11 | 12 | func init() { 13 | pkgpath = os.Getenv("GITHUB_WORKSPACE") 14 | if pkgpath == "" { 15 | pkgpath = os.Getenv("GOPATH") + "/src/github.com/otiai10/curr" 16 | } 17 | } 18 | 19 | func TestLine(t *testing.T) { 20 | Because(t, "Line() should provide current line", func(t *testing.T) { 21 | Expect(t, Line()).ToBe(21) 22 | // <- line 22 23 | Expect(t, Line()).ToBe(23) 24 | }) 25 | } 26 | 27 | func TestFunc(t *testing.T) { 28 | Because(t, "Func() should provide current function name", func(t *testing.T) { 29 | Expect(t, Foobaa()).ToBe("Foobaa") 30 | }) 31 | Expect(t, Func()).ToBe("TestFunc") 32 | } 33 | 34 | func TestFile(t *testing.T) { 35 | Because(t, "File() should provide current file name", func(t *testing.T) { 36 | Expect(t, File()).ToBe(pkgpath + "/all_test.go") 37 | }) 38 | } 39 | 40 | func TestBasename(t *testing.T) { 41 | Because(t, "Basename() should provide current file basename", func(t *testing.T) { 42 | Expect(t, Basename()).ToBe("all_test.go") 43 | }) 44 | } 45 | 46 | func TestDir(t *testing.T) { 47 | Because(t, "Dir() should provide current directory", func(t *testing.T) { 48 | Expect(t, Dir()).ToBe(pkgpath) 49 | }) 50 | } 51 | 52 | func Foobaa() string { 53 | return Func() 54 | } 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | curr 2 | ===== 3 | 4 | [![Go](https://github.com/otiai10/curr/actions/workflows/go.yml/badge.svg)](https://github.com/otiai10/curr/actions/workflows/go.yml) 5 | [![codecov](https://codecov.io/gh/otiai10/curr/branch/master/graph/badge.svg)](https://codecov.io/gh/otiai10/curr) 6 | [![Go Report Card](https://goreportcard.com/badge/github.com/otiai10/curr)](https://goreportcard.com/report/github.com/otiai10/curr) 7 | [![GoDoc](https://godoc.org/github.com/otiai10/curr?status.svg)](https://godoc.org/github.com/otiai10/curr) 8 | [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) 9 | [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fotiai10%2Fcurr.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fotiai10%2Fcurr?ref=badge_shield) 10 | 11 | Current file and dir privider for Golang. 12 | 13 | Just a sugar for [runtime](https://golang.org/pkg/runtime/). 14 | 15 | ```go 16 | import "curr" 17 | 18 | // __FILE__ 19 | f := curr.File() 20 | 21 | // __DIR__ 22 | d := curr.Dir() 23 | 24 | // __LINE__ 25 | l := curr.Line() 26 | 27 | // __FUNCTION__ 28 | fn := curr.Func() 29 | 30 | // basename(__FILE__) 31 | b := curr.Basename() 32 | ``` 33 | 34 | 35 | ## License 36 | [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fotiai10%2Fcurr.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fotiai10%2Fcurr?ref=badge_large) --------------------------------------------------------------------------------