Golang cannot write file into directory - go

Situation:
I'm trying to write a file into a directory, like shown as follows:
func (p *Page) Save() error {
filepath := DerivePath(p.Title)
fmt.Println(filepath)
content, _ := json.MarshalIndent(p, "", " ")
err := ioutil.WriteFile(filepath, content, 0600)
return err
}
Problem:
The following error occurs in line 5:
open data/Testpage.json: The system cannot find the path specified.
I already tried to create the file before writing with os.Create, but it doesn't work either.
Loading from the data directory works just fine. Only writing new files into the directory fails.
Additional information:
My project structure is as follows:
│ .gitignore
│ .project
│
├───bin
│ main.exe
│
├───data
│ Welcome.json
│
├───pkg
│ └───windows_amd64
│ page.a
│
├───src
│ ├───main
│ │ main.go
│ │
│ └───page
│ page.go
│ page_test.go
│
└───templates
view.html
As mentioned above, reading data/Welcome.json works just fine (by using io/ioutils.ReadFile).
The source is available on https://gitlab.com/thyaris/Wiki.
Executing D:\GitWorkspaces\Wiki\wiki>go test -v page writes the following output:
=== RUN TestSave
data/Testpage.json
--- FAIL: TestSave (0.00s)
page_test.go:15: open data/Testpage.json: The system cannot find the path specified.
page_test.go:19: 'Testpage.json' was not created
=== RUN TestLoadPage
--- FAIL: TestLoadPage (0.00s)
page_test.go:26: Error while loading
page_test.go:32: File content did not match
=== RUN TestDelete
--- PASS: TestDelete (0.00s)
FAIL
exit status 1
FAIL page 0.094s

Your problem here is that the test engine is not running your executable with the working directory you expect. Instead of using the working directory defined by your shell or IDE, it is setting it to the source directory of the code being tested. (I had this bite me too once, long ago :) I had almost forgotten about that...)
The simple solution is to change DerivePath so that you can set the prefix externally, then change it to a the path you need at the beginning of your tests. There are other (possibly better?) solutions of course.

Related

Export Variable in Linux Terminal with Multiple Strings Terraform

I have this variable in Terraform called "example". The variable is a type of "list(strings)" and I need to export that variable in my terminal, when running Terraform plan (i cannot put the value in code, so I have to export it):
export VAR_example=""xyz", "abc", "123""
but when I run this^, it gives me error:
│ Error: Invalid number literal
│
│ on <value for var.example> line 1:
│ (source code not available)
│
│ Failed to recognize the value of this number literal.
╵
╷
│ Error: Extra characters after expression
│
│ on <value for var.example> line 1:
│ (source code not available)
│
│ An expression was successfully parsed, but extra characters were found after it.
Then, I tried exporting it like this:
export VAR_example='"xyz", "abc", "123"'
Then, I got the error:
╷
│ Error: Extra characters after expression
│
│ on <value for var.example> line 1:
│ (source code not available)
│
│ An expression was successfully parsed, but extra characters were found after it.
Now, I am not sure how to export it at all. Any solution would be helpful.
Since it is a list of strings, you could provide it this way as well [1]:
terraform apply -var='examples=["xyz", "abc", "123"]'
However, since an environment variable needs to be used you could set it like:
export TF_VAR_examples='["xyz", "abc", "123"]'
Note that you need to prefix any environment variables with TF_ in order for Terraform to pick them up [2].
[1] https://www.terraform.io/language/values/variables#variables-on-the-command-line
[2] https://www.terraform.io/cli/config/environment-variables#tf_var_name

Ansible playbook which uses a role defined in a collection

This is an example of an Ansible playbook I am currently playing around with:
---
- hosts: all
collections:
- mynamespace.my_collection
roles:
- mynamespace.my_role1
- mynamespace.my_role2
- geerlingguy.repo-remi
The mynamespace.my_collection collection is a custom collection that contains a couple of roles, namely mynamespace.my_role1 and mynamespace.my_role2.
I have a requirements.yml file as follows:
---
collections:
- name: git#github.com:mynamespace/my_collection.git
roles:
- name: geerlingguy.repo-remi
version: "2.0.1"
And I install the collection and roles as follows:
ansible-galaxy collection install -r /home/ansible/requirements.yml --force
ansible-galaxy role install -r /home/ansible/requirements.yml --force
Each time I attempt to run the playbook it fails with the following error:
ERROR! the role 'mynamespace.my_role1' was not found in mynamespace.my_collection:ansible.legacy:/home/ansible/roles:/home/ansible_roles:/home/ansible
The error appears to be in '/home/ansible/play.yml': line 42, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
roles:
- mynamespace.my_role1
^ here
For the avoidance of doubt, I have tried multiple ways of defining the roles in the playbook including mynamespace.my_collection.my_role1 (the fully qualified name of the role within the collection).
I suspect I've done something wrong or misunderstood how it should work but my understanding is a collection can contain multiple roles and once that collection is installed, I should be able to call upon one or more of the roles within the collection inside my playbook to use it but it doesn't seem to be working for me.
The error seems to suggest it is looking for the role inside the collection but not finding it.
The collection is installed to the path /home/ansible_collections/mynamespace/my_collection and within that directory is roles/my_role1 and roles/my_role2.
Maybe the structure of the roles inside the collection is wrong?
I'm using Ansible 2.10 on CentOS 8.
Thanks for any advice!
EDIT:
I just wanted to expand on something I alluded to earlier. I believe the docs say the fully qualified name should be used to reference the role in the collection within the playbook.
Unfortunately, this errors too:
ERROR! the role 'mynamespace.my_collection.my_role1' was not found in mynamespace.my_collection:ansible.legacy:/home/ansible/roles:/home/ansible_roles:/home/ansible
The error appears to be in '/home/ansible/play.yml': line 42, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
roles:
- mynamespace.my_collection.my_role1
^ here
I posted this as an issue over at the ansible/ansible repo and we did get to the bottom of this.
One small clue missing is the contents of my /etc/ansible/ansible.cfg file:
COLLECTIONS_PATHS(/etc/ansible/ansible.cfg) = ['/home/ansible_collections']
DEFAULT_ROLES_PATH(/etc/ansible/ansible.cfg) = ['/home/ansible_roles']
To quote contributor Sloane Hertel directly:
There's a bug/discrepancy between how ansible-galaxy and ansible handle the collections path. ansible-galaxy tries to be smart and adds ansible_collections to the path if it's not there already, but ansible always joins ansible_collections to the path - the playbook is trying to find the collection in /home/ansible_collections/ansible_collections/.
The solution, therefore, is to change my COLLECTIONS_PATHS value from /home/ansible_collections to /home.
From then on ansible-playbook will be searching for any roles in collections in the path /home/ansible_collections/mynamespace/roles instead of /home/ansible_collections/ansible_collections/mynamespace/roles.
I changed my directory structure slightly:
home/
├─ ansible_collections/
│ ├─ mynamespace/
│ │ ├─ my_collection/
│ │ │ ├─ roles/
│ │ │ │ ├─ mynamespace
│ │ │ │ │ ├─ my_role1/
│ │ │ │ │ │ ├─ meta/
│ │ │ │ │ │ ├─ tasks/
Which now means my playbook file looks like:
---
- hosts: all
collections:
- mynamespace.my_collection
roles:
- mynamespace.my_role1
- mynamespace.my_role2
- geerlingguy.repo-remi
And the roles are now found correctly.
ansible playbook, was formerly known as converge. Try converting your workspace to yml file format for read only data structure.

Puppet unable to find my .erb template but finds other files OK?

No one has been able to explain this inside my company so if you are able to solve this KUDOS to you!
Inside my puppet repo I have setup as follows:
environment/ops/modules/papertrail
├── files
│ ├── elasticsearch_log_files.yml
│ ├── log_files.yml
│ └── remote_syslog.conf
|
└── manifests
├── elasticsearch.pp
└──init.pp
└── templates
└── elasticsearch_log_files.yml.erb
MY elasticsearch.pp file contains the following:
class papertrail::elasticsearch inherits papertrail {
$source = "puppet:///modules/papertrail"
file { "/etc/log_files.yml" :
mode => 0644,
owner => root,
group => root,
ensure => present,
source => "$source/elasticsearch_log_files.yml",
}
}
Now when I try to change the last line to:
"$source/elasticsearch_log_files.yml.erb",
or
"$source/templates/elasticsearch_log_files.yml",
Puppet errors out and says that it can't locate the file:
Error: /Stage[main]/Papertrail::Elasticsearch/File[/etc/log_files.yml]: Could not evaluate: Could not retrieve information from environment ops source(s) puppet:///modules/papertrail/elasticsearch_log_files.yml.erb
What is strange is that when I use the following stanza to just include the yml file instead of erb it works fine and the file gets populated on the target:
"$source/elasticsearch_log_files.yml",
How can I include my erb? I have dynamic variables that I need to assign to the configuration file log_files.yml and I am so far unable to do so =(
This is solved. I didn't add the template directory to my git commit so once added with git add . it worked.

Test output using curses

I'm trying to make a test using rspec for my simple othello app using curses. I need to make sure that the terminal prints an output as below:
To make the output more interactive, I use curses. When I check the output, it gives me a combination of ANSI sequence code (I guess):
[?1049h[1;23r(B[m[4l[?7h[?1h=[39;49m[?1h=[39;49m(B[m[H[2J[4;43H(B[0;1m[32m
┌───┬───┬───┬───┬───┬───┬───┬───┐[5;43H
│ │ │ │ │ │ │ │ │[6;43H
├───┼───┼───┼───┼───┼───┼───┼───┤[7;43H
│ │ │ │ │ │ │ │ │[8;43H
├───┼───┼───┼───┼───┼───┼───┼───┤[9;43H
│ │ │ │ │ │ │ │ │[10;43H
├───┼───┼───┼───┼───┼───┼───┼───┤[11;43H
│ │ │ │ │ │ │ │ │[12;43H
├───┼───┼───┼───┼───┼───┼───┼───┤[13;43H
│ │ │ │ │ │ │ │ │[14;43H
├───┼───┼───┼───┼───┼───┼───┼───┤[15;43H
│ │ │ │ │ │ │ │ │[16;43H
├───┼───┼───┼───┼───┼───┼───┼───┤[17;43H
│ │ │ │ │ │ │ │ │[18;43H
├───┼───┼───┼───┼───┼───┼───┼───┤[19;43H
│ │ │ │ │ │ │ │ │[20;43H
└───┴───┴───┴───┴───┴───┴───┴───┘[39m(B[m[20;43H[?[23;1H[?1049l[?1l>
How should I test if the result is like this?
You can redirect the output of a curses application to the standard output because that is the default for initscr (it helps if the application does not ask for input). If you capture the output into a file, you can do whatever analysis you need.
For the simple example given, curses is painting the screen once, using ANSI escape sequences for cursor movement (the parts ending with "H"), color (the ones with "m"), and a few others.
I said simple - if there were large blank areas on the screen, then curses would jump over those, and the residue without the escape sequences would look less like the actual screen. But since your lines are close together, it is less costly to just write the text with a few blanks as needed.
If you limit yourself to simple examples like that, then all you need to do is to strip the escape sequences out and compare the text. For a more general approach, you might consider making a screen-dump utility using winnstr (so that your application could dump the text from the screen as curses shows it).

Setting config when using 'make menuconfig'

I create a new config with my Kconfig, like this:
config VIDEO_MY_DRIVER
bool "my driver"
default y
depends on VIDEO_DEV && VIDEO_V4L2
select V4L2_MEM2MEM_DEV
---help---
This is a my driver
When I run 'make menuconfig' and when I search for 'CONFIG_VIDEO_MY_DRIVER', I See it.
Symbol: VIDEO_MY_DRIVER [=n]
│ Type : boolean
│ Prompt: my driver │
│ Location:
│ -> Device Drivers
│ (1) -> Multimedia support (MEDIA_SUPPORT [=y])
│ Defined at drivers/media/platform/mydriver/Kconfig:5
│ Depends on: MEDIA_SUPPORT [=y] && VIDEO_DEV [=n] && VIDEO_V4L2 [=n]
│ Selects: V4L2_MEM2MEM_DEV [=n]
│
But when I want to set it, I go to 'Device Drivers'-> 'Multimedia Support', I don't find the option to set it.
Can you please tell me if I make a mistake in my 'Kconfig' or where should I look for when I try to set it under 'Device Drivers'?
This link may help you get some info
It seems to me that for that option to appear, first check whether the dependencies of your module are enabled like here in your case is VIDEO_DEV and VIDEO_V4L2 . In your scenario it is still (=n) not included as part of your kernel source.

Resources