Skip to content

Commit

Permalink
Merge pull request #2 from dnnrly/feature/headers
Browse files Browse the repository at this point in the history
Add support for common HTTP headers
  • Loading branch information
dnnrly authored Jan 12, 2020
2 parents f7b2053 + cc1c028 commit 6f560d6
Show file tree
Hide file tree
Showing 7 changed files with 1,425 additions and 94 deletions.
46 changes: 46 additions & 0 deletions cmd/headers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"github.com/dnnrly/httpref"
"github.com/spf13/cobra"
)

// headersCmd represents the headers command
var headersCmd = &cobra.Command{
Use: "headers",
Aliases: []string{"header"},
Short: "References for common HTTP headers",
Long: `This displays useful information related to HTTP.
Most of the content comes from the Mozilla developer documentation (https://developer.mozilla.org/en-US/docs/Web/HTTP) and is copyright Mozilla and individual contributors. See https://developer.mozilla.org/en-US/docs/MDN/About#Copyrights_and_licenses for details.`,
Run: func(cmd *cobra.Command, args []string) {
results := httpref.Headers

if len(args) == 0 {
results = results.Titles()
} else {
results = results.ByName(args[0])
}

printResults(results)
},
}

func init() {
rootCmd.AddCommand(headersCmd)
}
42 changes: 26 additions & 16 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@ import (
"github.com/spf13/cobra"
)

var all = false
var titles = false

// rootCmd represents the base command when called without any subcommands
// rootCmd represents the base command when ctitlesed without any subcommands
var rootCmd = &cobra.Command{
Use: "httpref [item]",
Use: "httpref [filter]",
Args: cobra.MaximumNArgs(1),
Short: "Command line access to HTTP references",
Long: `This displays useful information related to HTTP.
Most of the content comes from the Mozilla developer documentation (https://developer.mozilla.org/en-US/docs/Web/HTTP) and is copyright Mozilla and individual contributors. See https://developer.mozilla.org/en-US/docs/MDN/About#Copyrights_and_licenses for details.`,
Most of the content comes from the Mozilla developer
documentation (https://developer.mozilla.org/en-US/docs/Web/HTTP)
and is copyright Mozilla and individual contributors. See
https://developer.mozilla.org/en-US/docs/MDN/About#Copyrights_and_licenses
for details.`,
RunE: root,
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
// Execute adds titles child commands to the root command and sets flags appropriately.
// This is ctitlesed by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
Expand All @@ -30,32 +35,37 @@ func Execute() {
}

func init() {
rootCmd.PersistentFlags().BoolVarP(&all, "all", "", all, "List all of the summaries available")
rootCmd.PersistentFlags().BoolVarP(&titles, "titles", "", titles, "List titles of the summaries available")
}

func root(cmd *cobra.Command, args []string) error {
results := httpref.Statuses
results := append(httpref.Statuses.Titles(), httpref.Headers.Titles()...)

if !all {
if !titles {
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "Must specify a status code, or part of it\n")
fmt.Fprintf(os.Stderr, "Must specify something to filter by\n")
os.Exit(1)
} else {
results = results.ByCode(args[0])
results = append(httpref.Statuses, httpref.Headers...)
results = results.ByName(args[0])
}
}

printResults(results)

return nil
}

func printResults(results httpref.References) {
switch len(results) {
case 0:
fmt.Fprintf(os.Stderr, "Code note recognised\n")
fmt.Fprintf(os.Stderr, "Name note recognised\n")
os.Exit(1)
case 1:
fmt.Printf("%s - %s\n\n%s\n", results[0].Code, results[0].Summary, results[0].Description)
fmt.Printf("%s - %s\n\n%s\n", results[0].Name, results[0].Summary, results[0].Description)
default:
for _, r := range results {
fmt.Printf("\t%s\t%s\n", r.Code, r.Summary)
fmt.Printf("\t%s\t%s\n", r.Name, r.Summary)
}
}

return nil
}
45 changes: 45 additions & 0 deletions cmd/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"github.com/dnnrly/httpref"
"github.com/spf13/cobra"
)

// statusCmd represents the status command
var statusCmd = &cobra.Command{
Use: "status [filter]",
Short: "References for HTTP status codes",
Long: `This displays useful information related to HTTP.
Most of the content comes from the Mozilla developer documentation (https://developer.mozilla.org/en-US/docs/Web/HTTP) and is copyright Mozilla and individual contributors. See https://developer.mozilla.org/en-US/docs/MDN/About#Copyrights_and_licenses for details.`,
Run: func(cmd *cobra.Command, args []string) {
results := httpref.Statuses

if len(args) == 0 {
results = results.Titles()
} else {
results = results.ByName(args[0])
}

printResults(results)
},
}

func init() {
rootCmd.AddCommand(statusCmd)
}
Loading

0 comments on commit 6f560d6

Please sign in to comment.