-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref: Refactor watcher system to use genericWatcher; improve project d…
…ocumentation (#20) * Refactor watcher system to use genericWatcher; improve docs * oops * Add examples to package docs
- Loading branch information
1 parent
fae2920
commit 68e7efa
Showing
11 changed files
with
331 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
go-metadata allows Go projects to easily interact with the Linode Metadata Service. | ||
Basic example: | ||
package main | ||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
metadata "github.com/linode/go-metadata" | ||
) | ||
func main() { | ||
// Create a new client | ||
client, err := metadata.NewClient(context.Background()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
// Retrieve metadata about the current instance from the metadata API | ||
instanceInfo, err := client.GetInstance(context.Background()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println("Instance Label:", instanceInfo.Label) | ||
} | ||
For more examples see: https://github.com/linode/go-metadata/tree/ref/watcher-refactor/examples | ||
To learn more about the Linode Metadata Service, see the official guide: https://www.linode.com/docs/products/compute/compute-instances/guides/metadata/?tabs=linode-api | ||
*/ | ||
|
||
package metadata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# go-metadata Examples | ||
|
||
This directory contains various runnable examples for using this package. | ||
|
||
- [Instance Watcher](./instancewatcher) - Watch for changes to the current Linode instance. | ||
- [Network Watcher](./networkwatcher) - Watch for changes to the current Linode instance's networking. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Instance Watcher Example | ||
|
||
This example makes use of the InstanceWatcher struct to watch for changes to the current Linode instance. | ||
|
||
### Running this Example | ||
|
||
NOTE: This example must be run from within a Linode instance. | ||
|
||
```bash | ||
go run main.go | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
metadata "github.com/linode/go-metadata" | ||
"log" | ||
"time" | ||
) | ||
|
||
func main() { | ||
ctx := context.Background() | ||
|
||
// Create a new client | ||
client, err := metadata.NewClient(ctx) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Create a new instance watcher | ||
instanceWatcher := client.NewInstanceWatcher( | ||
metadata.WatcherWithInterval(10 * time.Second), | ||
) | ||
|
||
// Start the network watcher in a goroutine. | ||
go instanceWatcher.Start(ctx) | ||
|
||
// Wait for changes | ||
for { | ||
select { | ||
case data := <-instanceWatcher.Updates: | ||
log.Printf( | ||
"Change to instance detected.\nNew data: %v\n", | ||
data, | ||
) | ||
case err := <-instanceWatcher.Errors: | ||
log.Fatalf("Got error from instance watcher: %s", err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Network Watcher Example | ||
|
||
This example makes use of the NetworkWatcher struct to watch for changes to the current Linode instance's networking. | ||
|
||
### Running this Example | ||
|
||
NOTE: This example must be run from within a Linode instance. | ||
|
||
```bash | ||
go run main.go | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
metadata "github.com/linode/go-metadata" | ||
"log" | ||
"time" | ||
) | ||
|
||
func main() { | ||
ctx := context.Background() | ||
|
||
// Create a new client | ||
client, err := metadata.NewClient(ctx) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Create a new network watcher | ||
networkWatcher := client.NewNetworkWatcher( | ||
metadata.WatcherWithInterval(10 * time.Second), | ||
) | ||
|
||
// Start the network watcher in a goroutine. | ||
go networkWatcher.Start(ctx) | ||
|
||
// Wait for changes | ||
for { | ||
select { | ||
case data := <-networkWatcher.Updates: | ||
log.Printf( | ||
"Change to network configuration detected.\nNew data: %v\n", | ||
data, | ||
) | ||
case err := <-networkWatcher.Errors: | ||
log.Fatalf("Got error from network watcher: %s", err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.