github.com/jonsyu1/godel@v0.0.0-20171017211503-64567a0cf169/docs/License-headers.md (about)

     1  Summary
     2  -------
     3  `./godelw license` updates the Go files in the project to have a specific license header based on configuration.
     4  
     5  Tutorial start state
     6  --------------------
     7  
     8  * `$GOPATH/src/github.com/nmiyake/echgo` exists and is the working directory
     9  * Project contains `godel` and `godelw`
    10  * Project contains `main.go`
    11  * Project contains `.gitignore` that ignores IDEA files
    12  * Project contains `echo/echo.go`, `echo/echo_test.go` and `echo/echoer.go`
    13  * `godel/config/dist.yml` is configured to build `echgo`
    14  * Project is tagged as 0.0.1
    15  * `godel/config/dist.yml` is configured to create distributions for `echgo`
    16  * Project is tagged as 0.0.2
    17  
    18  ([Link](https://github.com/nmiyake/echgo/tree/55182ff79dd28048782fb240920d6f2d90b453da))
    19  
    20  License
    21  -------
    22  
    23  Many open-source projects require specific license headers to be part of every source file. This can be enforced using
    24  the `license` task and configuration.
    25  
    26  First, add the license as a license file:
    27  
    28  ```
    29  ➜ curl http://www.apache.org/licenses/LICENSE-2.0.txt | sed '/./,$!d' > LICENSE
    30    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
    31                                   Dload  Upload   Total   Spent    Left  Speed
    32  100 11358  100 11358    0     0   150k      0 --:--:-- --:--:-- --:--:--  151k
    33  ```
    34  
    35  Run the following to configure a license header:
    36  
    37  ```
    38  ➜ echo 'header: |
    39    // Copyright (c) 2017 Author Name
    40    //
    41    // Licensed under the Apache License, Version 2.0 (the "License");
    42    // you may not use this file except in compliance with the License.
    43    // You may obtain a copy of the License at
    44    //
    45    //     http://www.apache.org/licenses/LICENSE-2.0
    46    //
    47    // Unless required by applicable law or agreed to in writing, software
    48    // distributed under the License is distributed on an "AS IS" BASIS,
    49    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    50    // See the License for the specific language governing permissions and
    51    // limitations under the License.' > godel/config/license.yml
    52  ```
    53  
    54  Run `./godelw license` to apply this license to all of the Go files in the project:
    55  
    56  ```
    57  ➜ ./godelw license
    58  ```
    59  
    60  Verify that this updated the Go files:
    61  
    62  ```
    63  ➜ git status
    64  On branch master
    65  Your branch is up-to-date with 'origin/master'.
    66  Changes not staged for commit:
    67    (use "git add <file>..." to update what will be committed)
    68    (use "git checkout -- <file>..." to discard changes in working directory)
    69  
    70  	modified:   echo/echo.go
    71  	modified:   echo/echo_test.go
    72  	modified:   echo/echoer.go
    73  	modified:   godel/config/license.yml
    74  	modified:   main.go
    75  
    76  Untracked files:
    77    (use "git add <file>..." to include in what will be committed)
    78  
    79  	LICENSE
    80  
    81  no changes added to commit (use "git add" and/or "git commit -a")
    82  ➜ cat echo/echo.go
    83  // Copyright (c) 2017 Author Name
    84  //
    85  // Licensed under the Apache License, Version 2.0 (the "License");
    86  // you may not use this file except in compliance with the License.
    87  // You may obtain a copy of the License at
    88  //
    89  //     http://www.apache.org/licenses/LICENSE-2.0
    90  //
    91  // Unless required by applicable law or agreed to in writing, software
    92  // distributed under the License is distributed on an "AS IS" BASIS,
    93  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    94  // See the License for the specific language governing permissions and
    95  // limitations under the License.
    96  
    97  package echo
    98  
    99  func NewEchoer() Echoer {
   100  	return &simpleEchoer{}
   101  }
   102  
   103  type simpleEchoer struct{}
   104  
   105  func (e *simpleEchoer) Echo(in string) string {
   106  	return in
   107  }
   108  ```
   109  
   110  Commit the changes to the repository:
   111  
   112  ```
   113  ➜ git add LICENSE echo godel main.go
   114  ➜ git commit -m "Add LICENSE and license headers"
   115  [master 0239b28] Add LICENSE and license headers
   116   6 files changed, 271 insertions(+)
   117   create mode 100644 LICENSE
   118  ```
   119  
   120  Tutorial end state
   121  ------------------
   122  
   123  * `$GOPATH/src/github.com/nmiyake/echgo` exists and is the working directory
   124  * Project contains `godel` and `godelw`
   125  * Project contains `main.go`
   126  * Project contains `.gitignore` that ignores IDEA files
   127  * Project contains `echo/echo.go`, `echo/echo_test.go` and `echo/echoer.go`
   128  * `godel/config/dist.yml` is configured to build `echgo`
   129  * Project is tagged as 0.0.1
   130  * `godel/config/dist.yml` is configured to create distributions for `echgo`
   131  * Project is tagged as 0.0.2
   132  * Go files have license headers
   133  
   134  ([Link](https://github.com/nmiyake/echgo/tree/0239b282904d05bb9eef6c3c3edfe1c28f888ad3))
   135  
   136  Tutorial next step
   137  ------------------
   138  
   139  [Go generate tasks](https://github.com/palantir/godel/wiki/Generate)
   140  
   141  More
   142  ----
   143  
   144  ### Remove license headers
   145  
   146  In some instances, it may be desirable to remove the license headers from all of the files. For example, if you are
   147  changing the type of license for the repository, you will want to remove all of the license headers that are already
   148  present before adding new headers.
   149  
   150  Run the following command:
   151  
   152  ```
   153  ➜ ./godelw license --remove
   154  ```
   155  
   156  Verify that this removed the headers:
   157  
   158  ```
   159  ➜ git status
   160  On branch master
   161  Your branch is ahead of 'origin/master' by 1 commit.
   162    (use "git push" to publish your local commits)
   163  Changes not staged for commit:
   164    (use "git add <file>..." to update what will be committed)
   165    (use "git checkout -- <file>..." to discard changes in working directory)
   166  
   167  	modified:   echo/echo.go
   168  	modified:   echo/echo_test.go
   169  	modified:   echo/echoer.go
   170  	modified:   main.go
   171  
   172  no changes added to commit (use "git add" and/or "git commit -a")
   173  ➜ cat echo/echo.go
   174  package echo
   175  
   176  func NewEchoer() Echoer {
   177  	return &simpleEchoer{}
   178  }
   179  
   180  type simpleEchoer struct{}
   181  
   182  func (e *simpleEchoer) Echo(in string) string {
   183  	return in
   184  }
   185  ```
   186  
   187  Revert these changes by running the following:
   188  
   189  ```
   190  ➜ git checkout -- echo main.go
   191  ```
   192  
   193  ### Specify custom license headers for specific paths
   194  
   195  In some instances, a project may contain certain files or directories that should have a different license header from
   196  other files -- for example, if a file or directory is based on a file from another project, it may be necessary to have
   197  a custom header to provide attribution for the original authors.
   198  
   199  Run the following command to remove the existing headers:
   200  
   201  ```
   202  ➜ ./godelw license --remove
   203  ```
   204  
   205  Once that is done, update the license configuration as follows:
   206  
   207  ```
   208  ➜ echo 'header: |
   209    // Copyright (c) 2017 Author Name
   210    //
   211    // Licensed under the Apache License, Version 2.0 (the "License");
   212    // you may not use this file except in compliance with the License.
   213    // You may obtain a copy of the License at
   214    //
   215    //     http://www.apache.org/licenses/LICENSE-2.0
   216    //
   217    // Unless required by applicable law or agreed to in writing, software
   218    // distributed under the License is distributed on an "AS IS" BASIS,
   219    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   220    // See the License for the specific language governing permissions and
   221    // limitations under the License.
   222  custom-headers:
   223    - name: echo
   224      header: |
   225        // Copyright 2017 Author Name. All rights reserved.
   226        // Licensed under the MIT License. See LICENSE in the project root
   227        // for license information.
   228      paths:
   229        - echo' > godel/config/license.yml
   230  ```
   231  
   232  This configuration specifies that the paths that match `echo` (which includes all paths within the `echo` directory)
   233  should use the custom header named "echo", while all of the other files should use the standard header. Run the
   234  following to apply the license and verify that it behaved as expected:
   235  
   236  ```
   237  ➜ ./godelw license
   238  ➜ cat echo/echo.go
   239  // Copyright 2017 Author Name. All rights reserved.
   240  // Licensed under the MIT License. See LICENSE in the project root
   241  // for license information.
   242  
   243  package echo
   244  
   245  func NewEchoer() Echoer {
   246  	return &simpleEchoer{}
   247  }
   248  
   249  type simpleEchoer struct{}
   250  
   251  func (e *simpleEchoer) Echo(in string) string {
   252  	return in
   253  }
   254  ➜ cat main.go
   255  // Copyright (c) 2017 Author Name
   256  //
   257  // Licensed under the Apache License, Version 2.0 (the "License");
   258  // you may not use this file except in compliance with the License.
   259  // You may obtain a copy of the License at
   260  //
   261  //     http://www.apache.org/licenses/LICENSE-2.0
   262  //
   263  // Unless required by applicable law or agreed to in writing, software
   264  // distributed under the License is distributed on an "AS IS" BASIS,
   265  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   266  // See the License for the specific language governing permissions and
   267  // limitations under the License.
   268  
   269  package main
   270  
   271  import (
   272  	"flag"
   273  	"fmt"
   274  	"strings"
   275  
   276  	"github.com/nmiyake/echgo/echo"
   277  )
   278  
   279  var version = "none"
   280  
   281  func main() {
   282  	versionVar := flag.Bool("version", false, "print version")
   283  	flag.Parse()
   284  	if *versionVar {
   285  		fmt.Println("echgo version:", version)
   286  		return
   287  	}
   288  	echoer := echo.NewEchoer()
   289  	fmt.Println(echoer.Echo(strings.Join(flag.Args(), " ")))
   290  }
   291  ```
   292  
   293  Revert these changes by running the following:
   294  
   295  ```
   296  ➜ git checkout -- echo godel
   297  ```